It would help us a lot if you provided some example input and some sample output too (what you want and what you actually get ).

Do you ids come in the same order in the two files? In whick case there is no need to read in the whole of the first file, but you can just compare the two files on a line by line basis.

I would guess that your main source of inefficiency comes from how you are doing your comparisons:

for my $ID1 (keys %bow1) { for my $ID2 (keys %bow2) { ...

This will iterate though every ID in the first hash and compare it with every id in the second hash! From the sounds of it you are only interested in comparing entries with matching IDs, so why not use a hash as it was intended and look up the appropriate ID?

Also I don't understand why you are storing the IDs twice (both as the key and as an entry in the value array ( $bow2{$ID2}[0] = $ID2; )

Lastly and very much OT, you should always check for success on filehandle operations:

open (my $fh2, '<', "$file2") || die "Failed to open $file2 for readin +g : $!"; ...do stuff... close $fh2 || die "Failed to close $file2 : $!";
Just a something something...

In reply to Re: how to compare two hashes with perl? by BioLion
in thread how to compare two hashes with perl? by FluffyBunny

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.