Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How best to compare hash values?

by choroba (Cardinal)
on May 05, 2010 at 21:31 UTC ( [id://838579]=note: print w/replies, xml ) Need Help??


in reply to How best to compare hash values?

Just go through all the keys in one hash and compare the values.
foreach my $key ( sort keys %hash1 ) { print $hash1{$key} - $hash2{$key}, "\n"; }
Problems may occur if a key is not present in both hashes, though.

Replies are listed 'Best First'.
Re^2: How best to compare hash values?
by doug (Pilgrim) on May 06, 2010 at 13:27 UTC

    I generally do it this way too, but I check all keys that are in both. Something like:

    my %union = ( %hash1, %hash2 ); my @keys = sort keys %union; undef %union; foreach my $key ( @keys ) { if ( ! exists $hash1{$key} ) { print "key $key is not in hash 1\n"; } elsif ( ! exists $hash2{$key} ) { print "key $key is not in hash 2\n"; } ....

    - doug

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://838579]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found