in reply to comparing 2 arrays of hashes

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