Here's the subroutine I came up with. Thanks for everyone who responded. I could never get the flattening the array method to work. It is not very elegant and probably slow, but this is the only solution I could find that prints the right answer:
equal
non equal
equal
#!/usr/bin/perl
$HASH_SIZE = 100;
%hash1;
%hash2;
for ($i=0; $i < $HASH_SIZE; $i++) {
$hash1{"i=$i"} = "j=$i";
}
%hash2 = %hash1;
if (&hasheq(\%hash1, \%hash2)) {
print "\nequal\n";
} else
{
print "\nnon equal\n";
}
$hash2{"anotherone"} = "anotherONE";
if (&hasheq(\%hash1, \%hash2)) {
print "\nequal\n";
} else
{
print "\nnon equal\n";
}
$hash1{"anotherone"} = "anotherONE";
if (&hasheq(\%hash1, \%hash2)) {
print "\nequal\n";
} else
{
print "\nnon equal\n";
}
sub hasheq {
my ($ha1, $ha2) = @_;
my %h1 = %$ha1;
my %h2 = %$ha2;
my @k1 = keys(%h1);
my @k2 = keys(%h2);
# do they have the same number of elements?
if (@k1 != @k2) {
return 0;
}
# are the keys the same?
if ((join '/' , sort @k1 ) ne (join '/' , sort @k2)) {
return 0;
}
# are the values the same?
if ( scalar grep { $h1{$_} ne $h2{$_} } @k1 )
{
return 0;
}
return 1;
}
|