在Perl中删除一行
问题描述:
我使用以下perl脚本在每一行上查找比较(并将它们打印出来)。我试图了解如何删除其中一个配音行(保存当前文件或输出到一个新的)。完整的脚本:http://pastebin.com/Vn1V0Uq2在Perl中删除一行
while (@lines) {
my ($line, @found) = shift @lines;
my $re = qr/\Q$$line{data}\E$/; # search token
@lines = grep { # extract matches from @lines
not $$_{data} =~ $re && push @found, $_
} @lines;
if (@found) { # write the report
print "line $$_{line}: $$_{data}\n" for $line, @found;
print "\n";
}
}
答
如果我理解你的权利,你试图删除包含早期的线条为子的一个任意行。如果是的话,这应该工作:
my @output;
foreach my $line (@lines) {
push @output, $line
unless grep index($line->{data}, $_->{data}) >= 0, @output;
}
编辑:我错过了你的“行”是hashrefs,而不是纯字符串的事实。固定。