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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.