Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

How do I test the deep equality of two hash tables?

by acser (Novice)
on Oct 26, 2001 at 02:19 UTC ( #121559=categorized question: print w/replies, xml ) Need Help??
Contributed by acser on Oct 26, 2001 at 02:19 UTC
Q&A  > hashes


See node: How to test equality of hashes?

Edit - Petruchio Tue Oct 30 17:35:05 UTC 2001: Corrected link format

Answer: How do I test the deep equality of two hash tables?
contributed by cmeyer

Another way to compare to arbitrarily complex data structures is to use canonical serialization. Storable is one module that provides this capability.

For example:

use Storable; sub deeply_equal { my ( $a_ref, $b_ref ) = @_; local $Storable::canonical = 1; return Storable::freeze( $a_ref ) eq Storable::freeze( $b_ref ); }
Answer: How do I test the deep equality of two hash tables?
contributed by fergal

Test::Deep provides deep comparisons of any data structures, including hashes. It is also capable of very advanced testing - what regular expressions do for strings, Test::Deep does for data structures.

use Test::Deep; if (eq_deeply($hash1, $hash2)) { print "they match" }

The fancy stuff comes in when you do something like

$animals = set("cat", "dog"); $people = set("john", "mary"); $a_p = set($animals, $people); print eq_deeply($structure, $a_p);
Ths is an example of testing set equality. It tests that $structure is an array with 2 elemenents (in any order) where each of those elements is an array with 2 elements, one containiing "cat" and "dog" (in any order) the other containing "john" and "mary" (in any order)

When used as part of the Test::Builder framework it gives diagnostics on where the structures differed.

Answer: How do I test the deep equality of two hash tables?
contributed by Roy Johnson

This does all the straightforward tests, and warns and returns undef on stuff it can't reliably compare (like code refs). Would probably be better to use UNIVERSAL::isa rather than ref.

sub deep_eq { my ($a, $b) = @_; if (not defined $a) { return not defined $b } elsif (not defined $b) { return 0 } elsif (not ref $a) { $a eq $b } elsif ($a eq $b) { return 1 } elsif (ref $a ne ref $b) { return 0 } elsif (ref $a eq 'SCALAR') { $$a eq $$b } elsif (ref $a eq 'ARRAY') { if (@$a == @$b) { for (0..$#$a) { my $rval; return $rval unless ($rval = deep_eq($a->[$_], $b->[$_ +])); } return 1; } else { return 0 } } elsif (ref $a eq 'HASH') { if (keys %$a == keys %$b) { for (keys %$a) { my $rval; return $rval unless ($rval = deep_eq($a->{$_}, $b->{$_ +})); } return 1; } else { return 0 } } elsif (ref $a eq ref $b) { warn 'Cannot test '.(ref $a)."\n"; un +def } else { return 0 } }

Please (register and) log in if you wish to add an answer

  • 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 contemplating the Monastery: (9)
    As of 2020-09-29 16:52 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (148 votes). Check out past polls.