Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How to check similarity of two hash tables

by Rajiv (Initiate)
on Apr 09, 2002 at 13:44 UTC ( [id://157719]=perlquestion: print w/replies, xml ) Need Help??

Rajiv has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

Perl Monks, Please Suggest: How can i find whether two hash tables have same and equal number of keys.

Originally posted as a Categorized Question.

  • Comment on How to check similarity of two hash tables

Replies are listed 'Best First'.
Re: How to check similarity of two hash tables
by RMGir (Prior) on Apr 09, 2002 at 14:21 UTC
    Of course, the obligatory one-liner (ok, one-expression) version:
    my $areEqual = (keys %hash1 == keys %hash2) && 0 == grep {!exists $hash2{$_}} keys %hash1;
Re: How to check similarity of two hash tables
by RMGir (Prior) on Apr 09, 2002 at 14:11 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://157719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2025-12-17 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (98 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.