始终是更好地使用,而不是使用管道Perl的grep的:
@lines = `zcat $file_list2`; # move output of zcat to array
die('zcat error') if ($?); # will exit script with error if zcat is problem
# chomp(@lines) # this will remove "\n" from each line
foreach $i (@contact_list) {
print "$i\n";
@ar = grep (/$i/, @lines);
print @ar;
# print join("\n",@ar)."\n"; # in case of using chomp
}
最好的办法是不是要求用zcat,但使用zlib库: http://perldoc.perl.org/IO/Zlib.html
use IO::Zlib;
# ....
# place your defiiniton of $file_list2 and @contact list here.
# ...
$fh = new IO::Zlib; $fh->open($file_list2, "rb")
or die("Cannot open $file_list2");
@lines = <$fh>;
$fh->close;
#chomp(@lines); #remove "\n" symbols from lines
foreach $i (@contact_list) {
print "$i\n";
@ar = grep (/$i/, @lines);
print (@ar);
# print join("\n",@ar)."\n"; #in case of using chomp
}
那编辑总结应该阅读“将代码添加到答案时请注意减价”。另外,欢迎来到Stack Overflow。 – simbabque
什么是变数?你为什么不使用'zgrep'? – tripleee
如果输入数据很大(如压缩格式所示),那么一次完成所有匹配似乎是一个更好的方法。 – tripleee