darkmoon has asked for the wisdom of the Perl Monks concerning the following question:
I need to use foreach loop to do comparison between two array of hashes. but I am not really know how to do this .
my original data:my hash1:NewData(file 1) Puma 77777 33333 44444 55555 Adidas 99999 88888 55555 77777 22222 11111 33333 44444 Brooks 11111 22222 33333 44444 33333 44444 55555 66666 OldData(file 2) Puma 77777 33333 44444 55555 Adidas 11111 11111 33333 44444 99999 88888 55555 77777 Brooks 11111 22222 33333 44444 33333 44444 55555 66666
my hash2:'Adidas' => { 'y1' => [ '88888', '11111' ], 'x2' => [ '55555', '33333' ], 'y2' => [ '77777', '44444' ], 'x1' => [ '99999', '22222' ] }, 'Puma' => { 'y1' => [ '33333' ], 'x2' => [ '44444' ], 'y2' => [ '55555' ], 'x1' => [ '77777' ] }, 'Brooks' => { 'y1' => [ '22222', '44444' ], 'x2' => [ '33333', '55555' ], 'y2' => [ '44444', '66666' ], 'x1' => [ '11111', '33333' ] } };
my attempt code for matching & non matching:$VAR1 = { 'Adidas' => { 'y1' => [ '11111', '88888' ], 'x2' => [ '33333', '55555' ], 'y2' => [ '44444', '77777' ], 'x1' => [ '11111', '99999' ] }, 'Puma' => { 'y1' => [ '33333' ], 'x2' => [ '44444' ], 'y2' => [ '55555' ], 'x1' => [ '77777' ] }, 'Brooks' => { 'y1' => [ '22222', '44444' ], 'x2' => [ '33333', '55555' ], 'y2' => [ '44444', '66666' ], 'x1' => [ '11111', '33333' ] } };
my output for matchingforeach my $newq (keys %hash1) { foreach my $oldq(keys %hash2) { if ( $newq eq $oldq) { foreach my $newx1(@{$hash1{$newq}{x1}}) { foreach my $oldx1(@{$hash2{$oldq}{x1}}) { if ($newx1 == $oldx1) { print "$newq\t$newx1\t$oldx1\n"; } if ($newx1 != $oldx1) { print "$newq\t$newx1\t$oldx1\n"; } }
My output for non matching :New Old Adidas 99999 99999 Puma 77777 77777 Brooks 11111 11111 Brooks 33333 33333
Desired output for matching:New Old Adidas 99999 11111 Adidas 22222 11111 Adidas 22222 99999 Brooks 11111 33333 Brooks 33333 11111
Not matchingNew Old x1 y1 x2 y2 x1 y1 x2 y2 Puma 77777 33333 44444 55555 77777 33333 44444 55555 Adidas 99999 88888 55555 77777 99999 88888 55555 77777 Brooks 11111 22222 33333 44444 11111 22222 33333 44444 Brooks 33333 44444 55555 66666 33333 44444 55555 66666
New Old x1 y1 x2 y2 x1 y1 x2 y2 Adidas 22222 11111 33333 44444 11111 11111 33333 44444
I only use x1 to do some testing on the code to see if it could work. Now i can get the correct matching for x1. But i get the wrong output for 'non matching' . my expected output of 'non matching' for x1 is Adidas 22222 11111only because 'x1=> 99999' are presented in both new data and old data. And i am not sure how to continue with the 'y1, x2, and y2' ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how to use foreach loop to do comparison between two arrays of hashes
by tybalt89 (Monsignor) on Oct 25, 2018 at 21:27 UTC | |
by BillKSmith (Monsignor) on Oct 27, 2018 at 18:51 UTC | |
by LanX (Saint) on Oct 27, 2018 at 18:57 UTC | |
|
Re: how to use foreach loop to do comparison between two arrays of hashes
by Laurent_R (Canon) on Oct 25, 2018 at 23:16 UTC | |
|
Re: how to use foreach loop to do comparison between two arrays of hashes
by 1nickt (Canon) on Oct 25, 2018 at 19:10 UTC | |
|
Re: how to use foreach loop to do comparison between two arrays of hashes
by BillKSmith (Monsignor) on Oct 25, 2018 at 20:54 UTC |