2017-03-06 82 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