pearlgirl has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks, I can't use new modules in our environment( don't ask..), so I need to write the code from scratch. So the structure of array 1 is like so:
{ 'name' -> orange, 'fruit' -> yes, 'vegetable' -> no, 'count' -> 50 }, { 'name' -> squash, 'fruit' -> no, 'vegetable' -> yes, 'count' -> 10 }, etc..
array 2:
{'name' -> orange, 'fruit' -> yes, 'vegetable' -> no, 'count' -> 50 'id' ->123 }, { 'name' -> squash, 'fruit' -> no, 'vegetable' -> yes, 'count' -> 10 'id' -> 222 }, etc..

I need to compare the values of each hash element in the array, and print a difference if the count is different and for what , ex. Apples: was 20, now 30. ( That one I figured out how to do). And then if one has array has more/less elements I need to print something like : Added: Vegetable:beet, count: 20, etc, or removed: Fruit:apple, count 30, etc. I swear my brain just freezes on this one. I'm exhausted. The part that I figured out is below. That's when both arrays have the same number of elements, and I'm just pulling the count difference if any. Note: The array elements are in alphabetical order. array2 is a reference to an array of hashes ( to complicate things even further),

for ( my $i =0; $i <=$#array1; $i ++) { foreach my $key (sort keys %{$array2[$i]}){ if ($array1[$i]{$key} ne $$array2[$i]{$key}) { print "changes"; } } }
How do I do the other comparison when the number of keys is different? I hope I was clear, I hope someone can help.. Thank you!

Replies are listed 'Best First'.
Re: comparing 2 arrays of hashes
by stevieb (Canon) on Apr 27, 2016 at 20:54 UTC

    I think the following does everything you want. Going forward, please post real working code/data. Your hash references had to be completely reworked in order to demo them.

    use warnings; use strict; my @a1 = ( { name => 'orange', fruit => 'yes', vegetable => 'no', count => 50 }, { name => 'squash', fruit => 'no', vegetable => 'yes', count => 10 }, ); my @a2 = ( { name => 'orange', fruit => 'yes', vegetable => 'no', count => 60, id =>123 }, { name => 'squash', fruit => 'no', vegetable => 'yes', count => 10, id => 222 }, ); my $count = 0; for my $first (@a1){ my $name = $first->{name}; my $second = $a2[$count]; for (keys %$second){ if (! exists $first->{$_}){ print "\@a2 has $_ for $name but \@a1 doesn't\n"; } } for my $k (keys %$first){ if (! exists $second->{$k}){ print "$k doesn't exist in \@a2 for $name\n"; next; } my $ne = 0; if ($first->{$k} =~ /^\d+$/){ if ($first->{$k} != $second->{$k}){ $ne = 1; } } else { if ($first->{$k} ne $second->{$k}){ $ne = 1; } } print "$k changed from $first->{$k} to $second->{$k} in $name\ +n" if $ne; } $count++; } } __END__ @a2 has id for orange but @a1 doesn't count changed from 50 to 60 in orange @a2 has id for squash but @a1 doesn't
Re: comparing 2 arrays of hashes (updated)
by haukex (Archbishop) on Apr 27, 2016 at 20:39 UTC

    Hi pearlgirl,

    One trick you can use to compare the keysets of two hashes is to delete a hash slice:

    my %hash1 = (a=>4,b=>5,c=>6,d=>7); my %hash2 = (a=>4,b=>5,c=>6,e=>7); # make a temp. copy of the keys of %hash1 my %h1k = map {$_=>1} keys %hash1; # delete from it all the keys present in %hash2 delete @h1k{keys %hash2}; # you're left with the keys that are only in %hash1 print "hash1 only: $_\n" for keys %h1k; # and vice versa for %hash2: my %h2k = map {$_=>1} keys %hash2; delete @h2k{keys %hash1}; print "hash2 only: $_\n" for keys %h2k; __END__ hash1 only: d hash2 only: e

    If the hashes are small, the copying of the keysets should not be a problem. (see also update below)

    There's also another trick, delete local (I haven't used this in production so I can't say if there might be caveats):

    my %hash1 = (a=>4,b=>5,c=>6,d=>7); my %hash2 = (a=>4,b=>5,c=>6,e=>7); { delete local @hash1{keys %hash2}; print "hash1 only: $_\n" for keys %hash1; } { delete local @hash2{keys %hash1}; print "hash2 only: $_\n" for keys %hash2; } # %hash1 and %hash2 are unchanged here __END__ hash1 only: d hash2 only: e

    Update: I find the first approach is particularly useful in situations where you don't need the hashes anymore afterwards, since then you don't need to make the temporary hash copies, you can just clobber the hashes directly and you only need one temporary copy of the keys:

    my %hash1 = (a=>4,b=>5,c=>6,d=>7); my %hash2 = (a=>4,b=>5,c=>6,e=>7); # make a temp. copy of the keys of %hash1 my @h1k = keys %hash1; # delete from %hash1 all the keys in %hash2 delete @hash1{keys %hash2}; # hash1 only has keys left that are unique to it print "hash1 only: $_\n" for keys %hash1; # delete from %hash2 all the keys from %hash1 delete @hash2{@h1k}; # you're left with the keys that are only in %hash2 print "hash2 only: $_\n" for keys %hash2; __END__ hash1 only: d hash2 only: e

    Hope this helps,
    -- Hauke D

Re: comparing 2 arrays of hashes
by Anonymous Monk on Apr 27, 2016 at 20:24 UTC

    First thought: print a YAML dump (because it sorts keys) of both arrays, then run a diff -u on them, changes should be obvious.