I am a beginner with Perl, and am trying to write a script to compare the two hashes and print the values in the first hash that are not found in the second. Although I know the script should be very simple, I am not sure why mine is not working. Any help would be much appreciated.
My script so far:
#!/usr/bin/perl
use strict;
use warnings;
use vars qw($a $b $c $d $hash1 %hash1 $info1 $hash2 %hash2);
open (FILE1, "<file1.txt") || die "$!\n Couldn't open file1.txt\n";
while (<FILE1>){
chomp (my $line=$_);
my ($a, $b, $c, $d) = split (/\t/, $line);
if ($a){
$hash1 -> {$a} -> {info1} = "$b\t$c\t$d";
}
$info1=$hash1->{$a}->{info1};
}
open (FILE2, "<file2.txt") || die "$!\n Couldnt open file2.txt \n";
open (Output, ">Output.txt")||die "Can't Open Output file";
while (<FILE2>) {
chomp (my $line=$_);
my ($a, $b, $c, $d) = split (/\t/, $line);
if ($a){
$hash2 -> {$a} -> {info2} = "$b\t$c\t$d";
}
foreach (my $hash1->{$a}) {
if (!exists $hash2{$a}) {
print Output "$a\t$info1\n";
}
}
}
close FILE1;
close FILE2;
close Output;
print "Done!\n";
I've found what appears to be multiple bugs in your program. I've reformatted your code and left comments to the precise locations of the bugs below: