in reply to How to divide the value of a hash key by the value of another hash key (when the keys are equivalent)?

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.

  • Comment on Re: How to divide the value of a hash key by the value of another hash key (when the keys are equivalent)?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to divide the value of a hash key by the value of another hash key (when the keys are equivalent)?
by aquinom (Acolyte) on Jun 06, 2011 at 18:20 UTC
    The initHash subroutine initializes all possible keys and sets the values to 0 to begin with though, I understand your change but I don't see how it's necessary?
    sub initHash { #init the all to all hash ##first argument is the hash of data, and the second is a referenc +e to all the columns my ($refHash, $refArr) = @_; foreach my $ele1(@$refArr){ foreach my $ele2(@$refArr){ my $key = $ele1 . "." . $ele2; if (exists $$refHash{$key}){ print STDERR "This key existed in your array of names, + skipping\n"; next; } else{ $$refHash{$key} = 0; } } } }
Re^2: How to divide the value of a hash key by the value of another hash key (when the keys are equivalent)?
by aquinom (Acolyte) on Jun 06, 2011 at 18:18 UTC
    I seem to be getting the expected output now, after fixing that silly typo in the $infile2 declaration