Re: [問題] 兩筆檔案比對
Code 1檢查a.txt每一行如果b.txt有這行,flag++
--------------------------------------------------------
open(FILE,"a.txt");@A=<FILE>;close(FILE);
open(FILE,"b.txt");@B=<FILE>;close(FILE);
foreach $line (@A)
{
next if ($line eq "\n");
$flag ++ if ( grep(/$line/,@B) );
}
print $flag;
-------------------------------------------------------
Code 2 a.txt每一行將對應的hash++,檢查b.txt每一行對應的
hash如果有值,則flag++
--------------------------------------------------------
open(FILE,"a.txt");@A=<FILE>;close(FILE);
open(FILE,"b.txt");@B=<FILE>;close(FILE);
($_ eq "\n")?1:$HASH{$_}++ foreach (@A);
($HASH{$_})?$flag++:1 foreach(@B);
print $flag;
-------------------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.74.242.13
→
05/06 00:10, , 1F
05/06 00:10, 1F
※ 編輯: imce 來自: 211.74.242.13 (05/06 00:16)
→
05/06 02:36, , 2F
05/06 02:36, 2F
討論串 (同標題文章)
Perl 近期熱門文章
PTT數位生活區 即時熱門文章