in reply to Comparing hashes without sorting the keys
Cheers!#!/usr/bin/perl -w use strict; my %aa=(uno=>1, due=>2, tres => 3); my %bb=(due=>2, uno=>1, tres => 3); print comphash(\%aa, \%bb) ? "Hashes are equal\n" : "Hashes are differ +ent\n"; sub comphash { my ($aa, $bb) = @_; return 0 unless keys %$aa == keys %$bb; my %whole_hash = (%$aa, %$bb); my $different; foreach (keys %whole_hash) { next if exists $aa{$_} && exists $bb{$_} && $aa{$_} == $bb{$_} +; $different++ && last; } return not $different; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Comparing hashes without sorting the keys
by BrowserUk (Patriarch) on Feb 20, 2004 at 03:40 UTC |