Hi All,
I am quite new to perl , I want to compare two text files , and output should the unique values in each files.
Using hash I am able to get unique values from each files but the output is not in order.
what should I do to get output in order ?
I am getting output as follows :-
In A hash but not B hash:
abc
abf
abcdef
In B hash but not A hash:
abf2
abcdef9
abco
I am expecting output as below,
In A hash but not B hash:
abc
abcdef
abf
In B hash but not A hash:
abco
abcdef9
abf2
I am quite new to perl , I want to compare two text files , and output should the unique values in each files.
Using hash I am able to get unique values from each files but the output is not in order.
what should I do to get output in order ?
Code:
use warnings;
@ARGV == 2 or die "Must specify 2 files!\n";
my $afile = shift;
my $bfile = shift;
my $ahash = make_hash($afile);
my $bhash = make_hash($bfile);
sub make_hash {
my $file = shift;
my %hash = ();
open IN, "<$file" or die "Can't open '$file': $!\n";
while (<IN>) {
chomp;
my ($key,$val) = split(/,/,$_);
$hash{$key} = $val;
}
return \%hash;
}
print "In A hash but not B hash:\n",
map {"$_\n"} grep {not exists $bhash->{$_}} keys %$ahash;
print "In B hash but not A hash:\n",
map {"$_\n"} grep {not exists $ahash->{$_}} keys %$bhash;
In A hash but not B hash:
abc
abf
abcdef
In B hash but not A hash:
abf2
abcdef9
abco
I am expecting output as below,
In A hash but not B hash:
abc
abcdef
abf
In B hash but not A hash:
abco
abcdef9
abf2
Comment