我是一个新手,编程,我希望有人可以给我讲解一下:要找到在Perl匹配和不匹配的值
所以,我有两个文本文件,即Scan1.txt和Scan2.txt存储在我的电脑。 Scan1.txt包含:
Tom
white
black
mark
john
ben
Scan2.txt包含:
bob
ben
white
gary
tom
black
patrick
我必须提取这两个文件和无与伦比的价值的匹配值,并分别打印出来。我以某种方式找到了这个工作正常的解决方案。但是有人能解释一下这场比赛究竟是怎么发生的。看起来好像只是这一行: $hash{$matchline}++
在代码中进行匹配,并在找到匹配项时增加散列值。我理解逻辑,但我不明白这场比赛是如何发生的。有人能帮助我理解这一点吗?
预先感谢您!
下面是代码:
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";