2017-03-06 81 views
0

背景:我沒有使用Perl的經驗,並且今天才開始,我試圖逐行比較兩個文件,找到不同的行並記錄它們是哪一行在一個單獨的文件。我還沒有那麼做,因爲我一次只添加一個功能。文件::比較的比較始終返回-1

錯誤:根據File :: Compare文檔,我正在對兩個文件句柄進行比較,並得到-1,這是一個錯誤。但是,我無法看到錯誤發生的位置。

代碼:

use strict; 
use warnings; 
use File::Compare; 

my $filename1 = 'test1.txt'; 
my $filename2 = 'test2.txt'; 
open(my $fh, '<:encoding(UTF-8)', $filename1) 
    or die "Could not open file '$filename1' $!"; 
open(my $fr, '<:encoding(UTF-8)', $filename2) 
    or die "Could not open file '$filename2' $!"; 

while() { 
    my $row1 = <$fh>; 
    my $row2 = <$fr>; 
    my $row1Num; 
    my $row2Num; 
    if ($row1 && $row2) { 
    chomp $row1; 
    chomp $row2; 
    $row1Num = substr($row1, 0, index($row1, ',')); 
    $row2Num = substr($row2, 0, index($row2, ',')); 
    while ($row1Num != $row2Num) { 
     if (!$row1 || !$row2) { 
     last; 
     } 
     if ($row1Num < $row2Num) { 
     #! Add row1Num to the list 
     print "$row1\n"; 
     $row1 = <$fh>; 
     if (!$row1) { 
      last; 
     } 
     chomp $row1; 
     $row1Num = substr($row1, 0, index($row1Num, ',')); 
     } else { 
     #! Add row2Num to the list 
     print "$row2\n"; 
     $row2 = <$fr>; 
     if (!$row2){ 
      last; 
     } 
     chomp $row2; 
     $row2Num = substr($row2, 0, index($row2Num, ',')); 
     } 
    } 
    } 
    if ($row1 && $row2) 
    { 
    if (compare($row1,$row2) != 0) 
    { 
     #! Add row1Num to the list 
     my $compare = compare($row1,$row2); 
     my $compare2 = compare($row2,$row1); 
     print "Compare($row1,$row2) == $compare\n"; 
     print "Compare($row2,$row1) == $compare2\n"; 
    } 
    } elsif (!$row1 && $row2) { 
    #! Add row2Num to the list 
    chomp $row2; 
    print "$row2\n"; 
    } elsif ($row1 && !$row2) { 
    #! Add row1Num to the list 
    chomp $row1; 
    print "$row1\n"; 
    } else { 
    last; 
    } 
} 
print "done\n"; 

輸出:

Compare(1,a,1,1) == -1 
Compare(1,1,1,a) == -1 
Compare(2,b,2,2) == -1 
Compare(2,2,2,b) == -1 
Compare(3,3,3,3) == -1 
Compare(3,3,3,3) == -1 
4,d 
5,5 
done 

test1.txt的:

1,a 
2,b 
3,3 
4,d 

的test2.txt:

1,1 
2,2 
3,3 
5,5 

如果有人能夠發現我是一個白癡,我會非常感激。

+0

試着鍵入'perldoc -q intersection'在你的命令行中得到一些關於如何完成它的想法。 –

+0

File :: Compare的'compare'需要將文件的名稱作爲參數進行比較。看起來你只需要'eq'運算符。 – ikegami

回答

0

ikegami注意到您的代碼中有關File::Compare的錯誤。這是一個使用散列的可能解決方案,%count

它記錄了從任一文件找到一行的次數。如果count == 1,那麼這行只在這兩個文件中看過一次 - 我認爲那是你想要的結果。

我沒有明確地打開每個文件,而是在命令行上提供了文件名。這允許while <>語法讀取這兩個文件。命令行會看起來像:

perl program_name.pl test1.txt test2.txt

(另請注意,我用的排序子程序,這將增加你的程序的速度,如果有一個大的高速緩存,(1000+),差異數。要排序的緩存是沒有必要的,但如果你有大量的項目進行排序)

#!/usr/bin/perl 
use strict; 
use warnings; 

my %count; 
$count{$_}++ while <>; 

print sort by_row_num grep $count{$_} == 1, keys %count; 

my %cache; 
sub by_row_num { 
    ($cache{$a} //= substr($a, 0, index($a, ','))) 
        <=> 
    ($cache{$b} //= substr($b, 0, index($b, ','))) 
} 

從樣本數據的輸出是非常有用:

1,a 
1,1 
2,2 
2,b 
4,d 
5,5