0

在进行一些解析后,我正在使用 open 3 并打印如下一行。我不想逐行打印 我想一次存储和打印 我该怎么做?

while(my $nextLine=<HANDLE_OUT>) {       
    chomp($nextLine);  
    if ($nextLine =~ m!<BEA-!){
        print "Skipping this line (BEA): |$nextLine|\n" if $debug;
    }
    print $nextLine."\n";
}
4

2 回答 2

1

如果您不希望在循环文件句柄时打印任何行,我会这样做:

数据结构是匿名数组(调试和输出)的散列。

my %handle_output = ( 
    debug => [], 
    output => [], 
); 


while(my $nextLine=<HANDLE_OUT>) {       
    chomp($nextLine);  
    if ($nextLine =~ m!<BEA-!){
       push( @{$handle_out{debug}}, $line ) if $debug;
    } else {
        push @{$handle_output{output}}, $line;
    }
}
for my $line ( @{$handle_output{output}} ) {
    print $line . "\n";
}
于 2013-05-14T14:08:12.653 回答
1

你只是想把它存储在一个变量中然后打印它?只需将其附加到循环中的变量即可。还是我误会了?

my $out = '';
while(my $nextLine=<HANDLE_OUT>) {       
    chomp($nextLine);  
    if ($nextLine =~ m!<BEA-!){
        print "Skipping this line (BEA): |$nextLine|\n" if $debug;
        next;  # I'm guessing you don't want to include these lines, either
    }
    $out .= $nextLine."\n";
}
print $out;
于 2013-05-14T13:47:50.763 回答