Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Answer: How to check similarity of two hash tables

by RMGir (Prior)
on Apr 09, 2002 at 14:11 UTC ( #157724=categorized answer: print w/replies, xml ) Need Help??

Q&A > hashes > How to check similarity of two hash tables - Answer contributed by RMGir

They're equal if, as you say, they have the same number of keys AND all the keys in one are in the other.
my $areEqual=1; if(keys %hash1 == keys %hash2) { foreach my $key(keys %hash1) { if(!exists $hash2{$key}) { $areEqual=0; last; } } } if($areEqual) { print "ARE "; } else { print "ARE NOT "; } print "equal\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2018-07-20 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (422 votes). Check out past polls.

    Notices?