0

让我重新开始。我有两个数组,它们是两个文本文件的行。我正在对第一个数组进行一些处理:

  1. 找到包含 STRING1 的行,并删除它和它之后的行。这已经实现了。

  2. 找到下一行是 just ,并从后面的行开始插入整个第二个数组

  3. 当我能做 2 的时候更多。:-)

4

3 回答 3

0

好吧,我真的很难理解你的意思。似乎@sam 和@psxls 有不同的输出并理解你的问题。但如果是我,我会使用perl readline函数直接处理文件拼接。无论如何,这并不难,只是一些简单的数组操作。

于 2013-10-30T07:59:24.177 回答
0

您可以使用splice将数组插入另一个数组:

#!/usr/bin/perl
use warnings;
use strict;

my @a=qw(a1 a2 a3 a4 <pre> a5 a6); # Let @a be the 1st array
my @b=qw(b1 b2 b3);                # Let @b be the 2nd array

my $index_a; # We will store here the index of the <pre> element from the 1st array
for (0..$#a) { # We iterate through all @a elements to find the position of <pre>
    if ($a[$_] eq '<pre>') {
        $index_a=$_;
        last;
    }
}
if (!defined $index_a) { # Simple validation that we did found a <pre> element
    die "<pre> not found";
}
splice(@a,$index_a,1,@b); # We insert the elements of @b into @a using splice
print join(' ',@a); # This will print "a1 a2 a3 a4 b1 b2 b3 a5 a6"
于 2013-10-30T00:01:51.927 回答
0

我不确定您的确切问题是什么,但如果我理解正确的话。

@array1 =(x, y, <pre>, c, d, e);
@array2 = (f, g, h);
@output = (c, d, e, f, g, h) 

If this is what you want

$element = shift (@array1);
while ($element ne '<pre>') {
      $element = shift (@array1);
}

@output = (@array1, @array2);
于 2013-10-30T03:29:10.880 回答