我是编程新手,希望有人可以向我解释一下:
所以我有两个文本文件,即 Scan1.txt 和 Scan2.txt 存储在我的计算机中。Scan1.txt 包含:
Tom
white
black
mark
john
ben
Scan2.txt 包含:
bob
ben
white
gary
tom
black
patrick
我必须提取这两个文件的匹配值和不匹配的值并分别打印。我以某种方式找到了解决方案,效果很好。但是有人可以解释一下比赛到底是怎么发生的。看起来有点像这一行:
$hash{$matchline}++
在代码中进行匹配并在找到匹配项时增加 hash 的值。我理解逻辑,但我不明白这场比赛是如何发生的。有人可以帮我理解这一点吗?
先感谢您!
这是代码:
open (F1, "Scan1.txt");
open (F2, "Scan2.txt");
%hash=();
while ($matchline= <F1> ){
$hash{$matchline}=1;
}
close F1;
while( $matchline= <F2> ){
$hash{$matchline}++;
}
close F2;
foreach $matchline (keys %hash){
if ($hash{$matchline} == 1){
chomp($matchline);
push(@unmatched, $matchline);
}
else{
chomp($matchline);
push (@matched, $matchline);
}
}
print "Matched Entries are >>\n";
print "```````````````````````\n";
print join ("\n", @matched) . "\n";
print "```````````````````````\n";
print "Unmatched Entries are >>\n";
print "```````````````````````\n";
print join ("\n", @unmatched) . "\n";
print "```````````````````````\n";