1

我有一个制表符分隔的文本文件,我在其中使用 while 循环根据某些列值进行一些过滤。我将输出写入一个新文件。现在我想将此输出文件用作另一个 while 循环的输入。这是我到目前为止的代码。一定有问题,因为out2.txt文件是空的。

my $table1 = $ARGV[0];
open( my $file, $table1 ) || die "$! $table1"; #inputfile
open( my $filter1, '+>', "out.txt" )  || die "Can't write new file: $!"; #first output
open( my $filter2, '+>', "out2.txt" ) || die "Can't write new file: $!"; #second output

while (<$file>) {
    my @line = split /\t/; #split on tabs
    if ( $line[12] =~ /one/ ) { 
        print $filter1 "$_";
    }   
}

while (<$filter1>) {
    my @line = split /\t/; #split on tabs
    if ( $line[11] =~ /two/ ) { 
        print $filter2 "$_";
    }   
}

输出文件out.txt包含正确的信息。然而out2.txt是空的。有人可以帮我解决这个问题吗?

4

1 回答 1

4

在您的脚本中,文件句柄的“光标”$filter1始终设置在文件的末尾,而您似乎想从文件的开头读取。seek您可以使用内置函数重置光标。

seek $filter1, 0, 0;  # reset $filter1 to start of file
while (<$filter1>) {
    ...
}
于 2013-08-05T15:29:29.070 回答