Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Compare two Hashes of Hashes

by kennethk (Abbot)
on Dec 16, 2016 at 15:57 UTC ( [id://1177930]=note: print w/replies, xml ) Need Help??


in reply to Compare two Hashes of Hashes

Can you explain why is_deeply and eq_or_diff won't work? What have you tried, and what didn't work? This sounds like a homework assignment, or at least an XY Problem. We are happy to help debug, but you won't learn if you don't struggle through.

Some basic discussion of how to run the comparison is a FAQ (How do I test whether two arrays or hashes are equal? in perlfaq4). You'll need to use two nested loops, in contrast to the example there.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: Compare two Hashes of Hashes
by Eshan_k (Acolyte) on Dec 16, 2016 at 17:04 UTC

    Thank you Kennethk for reply. Sorry I forgot to add what I tried. I made a function to compare first level key and value of two hashes but I am struggling to check for second level values. Can you please help?

    comp (\% hash1, \%hash2); sub comp { my $hash1 = shift; my $hash2 = shift; if( keys %{$hash1} != keys %{$hash2} ) { print "Hash1 has ", scalar(keys %{$hash1}), " units but hash2 has ", scalar(keys %{$hash2}), "\n"; } for my $key_hash1 (sort {lc($a) cmp lc($b) } keys %{$hash1} ) { if( ! exists $hash2->{$key_hash1} ) { print "Hash1 contains unit '$key_hash1' but hash2 has no such un +it\n"; next; } if( $hash1->{$key_hash1} ne $hash2->{$key_hash1} ) { print "Both hashes have '$key_hash1' as a unit ", "but hash1's value is '$hash1->{$key_hash1}' ", "and hash2's value is '$hash2->{$key_hash1}'\n"; } } }
      Thank you for posting that code. Note that with the block
      for my $key_hash1 (sort {lc($a) cmp lc($b) } keys %{$hash1} ) { if( ! exists $hash2->{$key_hash1} ) { print "Hash1 contains unit '$key_hash1' but hash2 has no such un +it\n"; next; }
      you will check if %hash2 is missing keys from %hash1, but you lack code to do the reverse check. So you probably want to add something like:
      for my $key_hash2 (sort {lc($a) cmp lc($b) } keys %{$hash2} ) { if( ! exists $hash1->{$key_hash2} ) { print "Hash1 contains unit '$key_hash2' but hash1 has no such un +it\n"; } }
      For adding another layer of checks, you need to replace your value check
      if( $hash1->{$key_hash1} ne $hash2->{$key_hash1} ) { print "Both hashes have '$key_hash1' as a unit ", "but hash1's value is '$hash1->{$key_hash1}' ", "and hash2's value is '$hash2->{$key_hash1}'\n"; }
      with another loop over the keys of the hashes, very much like you've done already.
      for my $key_hash1 (sort {lc($a) cmp lc($b) } keys %{$hash1} ) { if( ! exists $hash2->{$key_hash1} ) { print "Hash1 contains unit '$key_hash1' but hash2 has no such un +it\n"; next; } for my $key_hash1_tier2 (sort {lc($a) cmp lc($b) } keys %{$hash1-> +{$key_hash1}} ) { if( ! exists $hash2->{$key_hash1}{$key_hash1_tier2} ) { print "Hash1 $key_hash1 contains unit '$key_hash1_tier2' but h +ash2 has no such unit\n"; next; } if( $hash1->{$key_hash1}{$key_hash1_tier2} ne $hash2->{$key_hash +1}{$key_hash1_tier2} ) { print "Both hashes have '$key_hash1'-'$key_hash1_tier2' as a u +nit ", "but hash1's value is '$hash1->{$key_hash1}{$key_hash1_t +ier2}' ", "and hash2's value is '$hash2->{$key_hash1}{$key_hash1_t +ier2}'\n"; } } }
      subject to the same criticism I made before about not checking which elements are in %hash2 but not %hash1. You can see this clearly if you reverse the order of the arguments to comp.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-28 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found