You are still comparing the IDs twice (checking for key existence and then comparing if ( $bow1{$ID}[0] eq $bow2{$ID}[0] ){..., so you can streamline more there.

Also, you don't need to read in both files this way, but simply read in the first, then compare the ids and sequences of the second as you read them in, this will help you with memory issues is your files are huge.

Also, just as a bit of error checking, that you aren't overwriting any IDs, i would check for existence of the ID in the hash when you read in the file :

while (<$fh1>) { # Reading first hash my ($id, undef, undef, undef, $seq) = split; if ( exists $bow1{$ID} ){ warn "ID \'$id\' already exists for hash 1!\nThe current sequen +ce value is \'$bow1{$id}\', which would be replaced with \'$seq\'.\n" +; .... handle the error better (maybe ignore if sequences are the + same?) .... next; ## skip it } $bow1{$id} = $seq; } close $fh1 || die "Failed to close $file1 : $!";

Lastly, and this is just a matter of style, but this is important for writing code that will last and other people can read, in Perl variable names are usually all lower case, with words separated by an underscore ( <c>$this_that</\> ). All caps variables are reserved for global variables. This is just convention, but it makes your code easier to interpret, not just for others, but for yourself 6 months down the line...

Well done for working it out though, and thanks for posting it here!

Just a something something...

In reply to Re: UPDATE! I fixed it :D 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.