Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: andye Re: How to test equality of hashes?

by iakobski (Pilgrim)
on Jun 20, 2001 at 15:27 UTC ( [id://89968]=note: print w/replies, xml ) Need Help??


in reply to andye Re: How to test equality of hashes?
in thread How to test equality of hashes?

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://89968]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2024-04-18 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found