in reply to Re^2: Compare two Hashes of Hashes
in thread Compare two Hashes of Hashes
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_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 adding another layer of checks, you need to replace your value checkfor 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"; } }
with another loop over the keys of the hashes, very much like you've done already.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"; }
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.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"; } } }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|