我是一個新手,編程,我希望有人可以給我講解一下:要找到在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";