One immediate problem is that you are only reading 1 file
#your code #my $infile = $ARGV[0]; #my $infile2 = $ARGV[0]; <--- $infile == $infile2 #corrected code my $infile = $ARGV[0]; my $infile2 = $ARGV[1];
Update: found another issue
In the following code block you never initialize the value to 0 if the key does not exist.
#yourcode while (<INFILE>){ chomp; my @line = split ('\t', $_); my $key1 = 'A' . $line[0] . '.' . 'A' . $line[1]; ##first key my $key2 = 'A' . $line[1] . '.' . 'A' . $line[0]; ##key the other +way ##check to see if the key exists in the hash ##if it doesn't there is data in your infile, not in you names ar +ray if (exists $diplotypes{$key1} && $line[0] <= $line[1]) { $diplotypes{$key1} += $line[2]; } elsif (exists $diplotypes{$key2} && $line[0] >= $line[1]) { $diplotypes{$key2} += $line[2]; } else{##world is out to get you print STDERR "No key for $key1 or $key2\n"; next; } }
Something along the lines of the following might help your issue.
while (<INFILE>){ chomp; my @line = split ('\t', $_); my $key1 = 'A' . $line[0] . '.' . 'A' . $line[1]; ##first key my $key2 = 'A' . $line[1] . '.' . 'A' . $line[0]; ##key the other +way ##check to see if the key exists in the hash ##if it doesn't there is data in your infile, not in you names arr +ay ##new logic if($line[0] <= $line[1]) { if(exists $diplotypes{$key1}) { $diplotypes{$key1} += $line[2]; } else { #key doesnt exist so add it $diplotypes{$key1} = $line[2]; } } else { if(exists $diplotypes{$key2}) { $diplotypes{$key2} += $line[2]; } else { #key doesnt exist so add it $diplotypes{$key2} = $line[2]; } } }}
2nd Update: I did not notice that you had an initHashes function. That makes my 1st update unnecessary however what I posted is a more compact way of acheiving the same result. Sorry for the confusion.
Hope this helps.
In reply to Re: How to divide the value of a hash key by the value of another hash key (when the keys are equivalent)?
by zek152
in thread How to divide the value of a hash key by the value of another hash key (when the keys are equivalent)?
by aquinom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |