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:
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 hash1:
'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 hash2:
$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 attempt code for matching & non matching:
foreach 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 matching
New Old Adidas 99999 99999 Puma 77777 77777 Brooks 11111 11111 Brooks 33333 33333
My output for non matching :
New Old Adidas 99999 11111 Adidas 22222 11111 Adidas 22222 99999 Brooks 11111 33333 Brooks 33333 11111
Desired output for matching:
New 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
Not matching
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' ...


In reply to how to use foreach loop to do comparison between two arrays of hashes by darkmoon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.