始終是更好地使用,而不是使用管道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