我试图使用Perl来解析(基于C)程序的输出.每个输出行都是(1D)Perl数组,我有时想要存储(基于某些条件).
我现在希望(深)复制一个数组,当它的第一个元素有一个特定的关键字时,如果另一个关键字在后面的一个行数组中匹配,则打印相同的复制数组.
到目前为止,我尝试了以下方法:
#!/usr/bin/env perl
use strict; # recommended
use Storable qw(dclone);
...
while(1) # loop over the lines
{
# subsequent calls to tbse_line contain
# (references to) arrays of data
my $la = $population->tbse_line();
my @copy;
my $header = shift @$la;
# break out of the loop:
last if ($header eq 'fin');
if($header eq 'keyword')
{
@copy = @{ dclone \@$la };
}
if($header eq 'other_keyword')
{
print "second condition met, print first line:\n"
print "@copy\n";
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这会在屏幕上打印一个空行,而不是复制的数组的内容.我没有很多Perl经验,也无法弄清楚我做错了什么.
关于如何解决这个问题的任何想法?
my @copy分配@copy在当前范围内命名的新Perl数组.看起来您想要@copy在循环的一次迭代期间设置while并在不同的迭代中打印它.为了在每次新的while循环迭代开始时都不擦除您的数组,您应该将my @copy声明移到循环之外.
my @copy;
while (1) { ... }
Run Code Online (Sandbox Code Playgroud)