Lord Rau has asked for the wisdom of the Perl Monks concerning the following question:

I have to hashes A and B with various keys in common. I need to look at the value for each key and determine if that value is the same in both hashes. The problem is some values are scalars and some are references to anonymous arrays. It may be possible that a key in A is a scalar and that same key in B is a list or vise versa (see table below). Also if, A or B or Both is an Array, I need to see if each specific value of A is in list B, not the entire list (order inside the hash is not important).
A | B =================== Array | Array Scalar | Array Array | Scalar Scalar | Array

Replies are listed 'Best First'.
Re: Searching out values in Hash
by Fastolfe (Vicar) on Oct 02, 2000 at 23:53 UTC
    You may be interested in Set::Scalar for doing set memberships (determining of @a is a subset of @b for example), but that may be a bit much, since apparently you have to build set objects out of your data; you can't just do set membership functions on the arrays themselves.

    I also have a tickling in the back of my brain about the possibility that there's a module out there specifically for recursing down data structures and determining if two of them are equal. I'll see if I can find such a thing and I'll update this post if I do. If someone else finds something like that, please let us know.

    Update: Found it. Data::Compare is what you want. Also, my code below will return true even if your arrays are in different orders, since it's incorrectly examining the presence of each item, and not the true structure of the array. Use Data::Compare.

    Otherwise, this code might do what you're looking for:

    use strict; sub hash_equality (\%\%) { my %hash1 = %{shift()}; my %hash2 = %{shift()}; my %done; foreach my $key (keys %hash1, keys %hash2) { next if $done{$key}++; if (!ref($hash1{$key})) { return if $hash1{$key} ne $hash2{$key}; } elsif (ref($hash1{$key}) eq "ARRAY") { my ($e, %union, %isect); foreach $e (@{$hash1{$key}}, @{$hash2{$key}}) { $union{$e}++ && $isect{$e}++; } return if scalar(keys %union) != scalar(keys %isect); } else { # handle other reference types if needed? return; } } # ok return 1; } my %A = ( one => 1, two => [ qw{ t w o } ] ); my %B = ( one => 1, two => [ qw{ t r e } ] ); my %C = ( one => 1, two => [ qw{ t w o } ] ); print "Mismatch!\n" unless hash_equality(%A, %B); print "OK!\n" if hash_equality(%A, %C);