pearlgirl has asked for the wisdom of the Perl Monks concerning the following question:
array 2:{ 'name' -> orange, 'fruit' -> yes, 'vegetable' -> no, 'count' -> 50 }, { 'name' -> squash, 'fruit' -> no, 'vegetable' -> yes, 'count' -> 10 }, etc..
{'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),
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!for ( my $i =0; $i <=$#array1; $i ++) { foreach my $key (sort keys %{$array2[$i]}){ if ($array1[$i]{$key} ne $$array2[$i]{$key}) { print "changes"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: comparing 2 arrays of hashes
by stevieb (Canon) on Apr 27, 2016 at 20:54 UTC | |
|
Re: comparing 2 arrays of hashes (updated)
by haukex (Archbishop) on Apr 27, 2016 at 20:39 UTC | |
|
Re: comparing 2 arrays of hashes
by Anonymous Monk on Apr 27, 2016 at 20:24 UTC |