Gunnar Hjalmarsson said:
If you encounter problems, please feel welcome to ask for help here,
but now we want to see code that you wrote.
Ok guys, I've got a slightly different requirement now. The lists I
have to compare have increased to 3 and I need to find out words that
appear in at least two of the lists. However, if a word appears in
only 2 list, I need to include it in my third list by giving it a
default value, like '0'.
Example:
ListA
Apple 4, Boy 3, Cat 5
ListB
Apple 1.0, Baby 2.1, Cat 3.3
ListC
Apple 99, Beef 100, Cow 101
Should give me as result:
ListA ListB ListC
Apple 4 Apple 1.0 Apple 99
Cat 5 Cat 3.3 Cat 0
Right now, my code looks like below but I don't know how to give this
default value. Any help is much appreciated.
my %ListA;
open my $fh, '< ListA.txt' or die "Couldn't open ListA.txt $!";
while (<$fh>) {
for (split /,\s*/) {
my ($key, $value) = split;
$ListA{$key} = $value;
}
}
close $fh;
my %ListB;
open my $fh, '< ListB.txt' or die "Couldn't open ListB.txt $!";
while (<$fh>) {
for (split /,\s*/) {
my ($key, $value) = split;
$ListB{$key} = $value;
}
}
close $fh;
my %ListC;
open my $fh, '< ListC.txt' or die "Couldn't open ListC.txt $!";
while (<$fh>) {
for (split /,\s*/) {
my ($key, $value) = split;
$ListC{$key} = $value;
}
}
close $fh;
for (keys %ListA) {
delete $ListA{$_} unless exists $ListB{$_} || $ListC{$_} ;
}
for (keys %ListB) {
delete $ListB{$_} unless exists $ListA{$_} || $ListC{$_};
}
for (keys %ListC) {
delete $ListC{$_} unless exists $ListA{$_} || $ListB{$_};
}
print "ListA \n";
print "$_\t$ListA{$_}\n" for sort keys %ListA;
print "\n";
print "ListB \n";
print "$_\t$ListB{$_}\n" for sort keys %ListB;
print "\n";
print "ListC \n";
print "$_\t$ListC{$_}\n" for sort keys %ListC;