in reply to andye Re: How to test equality of hashes?
in thread How to test equality of hashes?
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:
So flatten needs to be: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. ======== +==
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
|
|---|