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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.