in reply to
Re: Re: How to test equality of hashes?
in thread How to test equality of hashes?
Nice. I can't see this kind of follow-ups often enough!
A few comments, naturally:
- use strict warnings and diagnostics or die.
These are your friends on the long run.
- You declare 2 vars without need:
my %h1 = %{ $_[0] }; #h2 similar
- Check whether your refs are defined:
my %h1 = (defined $_[0] and ref $_[0]) ? %{ $_[0] } : ();
This will make the code more portable/general/cleaner. You
may add an extra check for hashiness.
- I just realized that this code can't distinguish between
undefined or empty, so the grep becomes:
scalar grep {
my ($a, $b) = ( $h1{$_}, $h2{$_} );
( not defined $a and defined $b )
or
( not defined $b and defined $a )
or
$a ne $b
} @k1
Had to think extra carefully there!
Hope this helps,
Jeroen