我有2个文件,即
**a.txt**
cars
bikes
bus
vehicle
atv
**b.txt**
hawk
hero
atv
bus
***result.txt***
cars
bikes
vehicle
hawk
hero
我想打印两个文件之间的差异。现在我尝试了一个代码,但它以随机的方式给了我差异;我希望它以正确的顺序方式显示它。任何人都可以帮我解决这个问题。
use strict;
my %results = ();
open FILE1, "<a.txt"
or die "Could not open file: $! \n";
while (my $line = <FILE1>) {
$results{$line}=1;
}
close FILE1;
open FILE2, "<b.txt"
or die "Could not open file: $! \n";
while (my $line = <FILE2>) {
$results{$line}++;
}
close FILE2;
open OUTFILE, ">>result.txt"
or die "Cannot open $outfile for writing \n";
foreach my $line (keys %results) {
print OUTFILE $line if $results{$line} == 1;
}
close OUTFILE;
close OUTFILE1;
我用这段代码得到的输出是
***result.txt***
cars
hawk
bikes
hero
vehicle