Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

andye Re: How to test equality of hashes?

by andye (Curate)
on Jun 20, 2001 at 14:17 UTC ( [id://89957]=note: print w/replies, xml ) Need Help??


in reply to How to test equality of hashes?

How about this?
sub flatten {return "@_"}; if ( flatten(%hash1) eq flatten(%hash2) ) { print "equal\n" } else { print "not\n" }
This will compare the keys as well as the values, so if the same values are fixed to different keys then it should still return false. The 'stringification warning' above applies, i.e. this might have a problem with fractional numbers. Should be ok with strings and integers though.

andy.

PS If anyone can tell me how to get this hash-flattened-into-an-array behaviour without a sub, I'd be interested. I've been fiddling around with the syntax trying to get that.

PPS Can two hashes with identical keys and values ever store the keys in a different order? I've been assuming not, but if they can, then the above won't work reliably. Again, would be interested in info... or a pointer to the correct RTFM... on this.

Replies are listed 'Best First'.
(ar0n) Re (2): How to test equality of hashes?
by ar0n (Priest) on Jun 20, 2001 at 16:06 UTC
    PS If anyone can tell me how to get this hash-flattened-into-an-array behaviour without a sub, I'd be interested. I've been fiddling around with the syntax trying to get that.
    if ( "@{[%hash1]}" eq "@{[$hash2]}" ) { # foo ... }


    ar0n ]

      None of these repsonses sort the array. I would have thought that this solution would be flakey unsorted since hashs can come out in any order they please...

      I do flattening for comparison by shoving a hashref into an array ( @foo = %{$foo} ) and then sorting the array ( @foo = sort @foo ) and flattening that into a string.,p> The obvious problem with this is that if you have a hash where the keys/values were transposed then it would still appear equal. Similar situations will occur with other data anomolies.

      Not a problem in my case, but worth remembering.

Re: andye Re: How to test equality of hashes?
by iakobski (Pilgrim) on Jun 20, 2001 at 15:27 UTC
    Neat solution.

    I was going to say that they always come out in the same order because the hashing key does not change. Unfortunately that did not stand up to testing. The order the elements are inserted into the hash makes a difference:

    my ( %hash1, %hash2 ); $hash1{"value$_"} = $_ for ( 1 .. 11 ); $hash2{"value$_"} = $_ for ( 5 .. 11 ); $hash2{"value$_"} = $_ for ( 1 .. 4 ); sub flatten { return "@_" } if ( flatten(%hash1) eq flatten(%hash2) ) { print "equal\n". flatten( %hash1 ) . "\n" . flatten(%hash2) } else { print "not\n" . flatten( %hash1 ) . "\n" . flatten(%hash2); } not value10 10 value11 11 value1 1 value2 2 value3 3 value4 4 value5 5 val +ue6 6 value7 7 value8 8 value9 9 value10 10 value1 1 value11 11 value2 2 value3 3 value4 4 value5 5 val +ue6 6 value7 7 value8 8 value9 9 ========== [C:\users\jake\code\komodo\test3.pl] run finished. ======== +==
    So flatten needs to be:
    sub flatten { my %hash = @_; return join '', map "$hash{$_}$_", sort keys %hash; }

    The answer to your question on how not to use a sub would be to use the join, map in the if clause. This saves passing the hash by value, but duplicates the logic on either side of the eq

    -- iakobski

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://89957]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 10:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found