我已經嘗試了所有步驟,以便將與一個文件相比唯一存在的單詞列表與另一個文件進行比較。我在代碼中進行了一些調試打印,以確定它發生的位置,並發現代碼在比較循環中從不執行任何操作。比較兩個單詞列表並保存不在第二個列表中的單詞perl
我想我是盲目的或忽略了一些非常明顯的東西 - 有人請指出什麼是錯的,並且喜歡嘲笑我的「可能是新手」的錯誤。
while (<IN>) { #read the file
chomp;
$_ = lc; #convert to lower case
s/ --//g; #remove double hyphen dashes
s/ -//g; #remove single hyphen dashes
s/ +/ /g; #replace multiple spaces with one space
s/[~`@#$%^&*-+=<>.,:;?"!_()\[\]]//g; #remove punctuation
@hwords = split;
# foreach $w (@hwords) { print "$w \n";}
}
while (<IN0>) { #read the file
chomp;
$_ = lc; #convert to lower case
s/ --//g; #remove double hyphen dashes
s/ -//g; #remove single hyphen dashes
s/ +/ /g; #replacxew multiple spaces with one space
s/[~`@#$%^&*-+=<>.,:;?"!_()\[\]]//g; #remove punctuation
@awords = split;
# foreach $w (@awords) {print "$w\n";}
}
$count =0;
@unique =();
print "got here!\n"; # YES - it gets here
foreach $w (@hwords) { print "$w \n";}
foreach $h (@hwords) {
$x=1;
print "got there!\n"; # NOPE, doesn't get here
foreach $a (@awords) {
if ($h eq $a) {
$x=0;
print "equals\n"; # NEVER see this
}
}
if ($x eq 1) {
++$count;
@unique = @unique, $h;
print "$count, $h\n"; # NEVER see this, either
}
}
需要注意的是正確,你需要用4個空格縮進在編輯器窗口中顯示的代碼。我爲你編輯了這一個 – stevieb
我會推薦'perltidy',以便讓縮進一致。 – Sobrique