views:

2866

answers:

5

I'm wondering if there's an idiomatic one-liner or a standard-distribution package/function that I can use to compare two Perl hashes with only builtin, non-blessed types. The hashes are not identical (they don't have equivalent memory addresses).

I'd like to know the answer for both for shallow hashes and hashes with nested collections, but I understand that shallow hashes may have a much simpler solution.

TIA!

+8  A: 

Something like cmp_deeply available in Test::Deep ?

codelogic
Sure, I think Test::Deep::eq_deeply is exactly what I'm looking for!
cdleary
+1  A: 

I don't know if there's an easy way or a built-in package, and I don't know what happens when you just do %hash1 == %hash2 (but that's probably not it), but it's not terribly hard to roll your own:

sub hash_comp (\%\%) {
  my %hash1 = %{ shift };
  my %hash2 = %{ shift };
  foreach (keys %hash1) {
    return 1 unless defined $hash2{$_} and $hash1{$_} == $hash2{$_};
    delete $hash1{$_};
    delete $hash2{$_};
  }
  return 1 if keys $hash2;
  return 0;
}

Untested, but should return 0 if the hashes have all the same elements and all the same values. This function will have to be modified to account for multidimensional hashes.

If you want something from a standard distribution, you could use Data::Dumper; and just dump the two hashes into two scalar variables, then compare the strings for equality. That might work.

There's also a package on CPAN called FreezeThaw that looks like it does what you want.

Note that to use the smart match (not repeated here because it's already posted), you will have to use feature; and it's only available for Perl 5.10. But who's still using Perl 5.8.8, right?

Chris Lutz
"But who's still using Perl 5.8.8, right?" I am. :-)
cdleary
Also the "==" comparison forces a scalar context, so it compares number of elements.
cdleary
@cdleary: I said that because, as a user of Mac OS X, all OS X users are by default still using 5.8.8, unless they went out of their way to upgrade it.
Chris Lutz
A: 

[This was a response to an answer by someone who deleted their answer.]

Uh oh!

%a ~~ %b && [sort values %a] ~~ [sort values %b]

doesn't check whether the values belong to the same keys.

#! perl
use warnings;
use strict;

my %a = (eat => "banana", say => "whu whu"); # monkey
my %b = (eat => "whu whu", say => "banana"); # gorilla
print "Magilla Gorilla is always right\n" 
    if %a ~~ %b && [sort values %a] ~~ [sort values %b];
A: 

For shallow hashes:

(grep {exists %hash2{$_}} keys %hash1) > 0
Prolix
A: 

hashes can be casted into arrays, where every value follows its key (but you won't know the order of the keys). So:

( join("",sort(%hash1)) eq join("",sort(%hash2)) )

Oh, wait, that won't work because there are some edge cases, like:

%hash1 = { 'aaa' => 'aa'  };
%hash2 = { 'aa'  => 'aaa' };

So it's best to use a character in the join() that you KNOW will never appear in any key or value. If the values are BLOBs, that will be a big problem, but for anything else you could use the NULL char "\0".

( join("\0",sort(%hash1)) eq join("\0",sort(%hash2)) )

Looks kinda ugly, I know, but it will do for checking if two hashes are equal in a shallow way, which is what most people are looking for.

Moses Moore