Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Are you looking for different keys, keys/value pairs or both?

One option is iterate through both hashes in-sync (much like the "merge" step in a mergesort) and spit out any differences. What I present below is meant as more of an algorithm than an actual implementation (though it does work). It could be tweaked quite a bit in actual implementation to get much better performance. This is probably not the best perl implementation, but it is a good general-purpose algorithm for doing "diffrence of lists/difference of hashes".

my @k1=sort keys %h1; my @k2=sort keys %h2; my $k1; my $k2; while((scalar @k1 > 0)&&(scalar @k2 > 0)){ if(!defined $k1){ $k1=shift @k1; } if(!defined $k2){ $k2=shift @k2; } if($k1 eq $k2){ if($h1{$k1} ne $h2{$k2}){ # .. keys match but contents dont } undef $k1; undef $k2; }elsif($k1 lt $k2){ # .. key in k1 and not in k2 undef $k1; }else{ # .. key in k2 and not in k1 undef $k2; } } if(defined $k1){ foreach ($k1,@k1){ # .. key in k1 and not in k2 } } if(defined $k2){ foreach ($k2,@k2){ # .. key in k2 and not in k1 } }

In reply to RE: diff of two hashes. by lhoward
in thread diff of two hashes. by ChuckularOne

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (7)
    As of 2019-07-18 15:54 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found