Hi pearlgirl,

One trick you can use to compare the keysets of two hashes is to delete a hash slice:

my %hash1 = (a=>4,b=>5,c=>6,d=>7); my %hash2 = (a=>4,b=>5,c=>6,e=>7); # make a temp. copy of the keys of %hash1 my %h1k = map {$_=>1} keys %hash1; # delete from it all the keys present in %hash2 delete @h1k{keys %hash2}; # you're left with the keys that are only in %hash1 print "hash1 only: $_\n" for keys %h1k; # and vice versa for %hash2: my %h2k = map {$_=>1} keys %hash2; delete @h2k{keys %hash1}; print "hash2 only: $_\n" for keys %h2k; __END__ hash1 only: d hash2 only: e

If the hashes are small, the copying of the keysets should not be a problem. (see also update below)

There's also another trick, delete local (I haven't used this in production so I can't say if there might be caveats):

my %hash1 = (a=>4,b=>5,c=>6,d=>7); my %hash2 = (a=>4,b=>5,c=>6,e=>7); { delete local @hash1{keys %hash2}; print "hash1 only: $_\n" for keys %hash1; } { delete local @hash2{keys %hash1}; print "hash2 only: $_\n" for keys %hash2; } # %hash1 and %hash2 are unchanged here __END__ hash1 only: d hash2 only: e

Update: I find the first approach is particularly useful in situations where you don't need the hashes anymore afterwards, since then you don't need to make the temporary hash copies, you can just clobber the hashes directly and you only need one temporary copy of the keys:

my %hash1 = (a=>4,b=>5,c=>6,d=>7); my %hash2 = (a=>4,b=>5,c=>6,e=>7); # make a temp. copy of the keys of %hash1 my @h1k = keys %hash1; # delete from %hash1 all the keys in %hash2 delete @hash1{keys %hash2}; # hash1 only has keys left that are unique to it print "hash1 only: $_\n" for keys %hash1; # delete from %hash2 all the keys from %hash1 delete @hash2{@h1k}; # you're left with the keys that are only in %hash2 print "hash2 only: $_\n" for keys %hash2; __END__ hash1 only: d hash2 only: e

Hope this helps,
-- Hauke D


In reply to Re: comparing 2 arrays of hashes (updated) by haukex
in thread comparing 2 arrays of hashes by pearlgirl

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.