Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

how to use foreach loop to do comparison between two arrays of hashes

by darkmoon (Novice)
on Oct 25, 2018 at 18:12 UTC ( [id://1224689]=perlquestion: print w/replies, xml ) Need Help??

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:
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' ...

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

    All it takes is a couple of regexes :)

    #!/usr/bin/perl # https://perlmonks.org/?node_id=1224689 use strict; use warnings; my $newdata = <<END; Puma 77777 33333 44444 55555 Adidas 99999 88888 55555 77777 22222 11111 33333 44444 Brooks 11111 22222 33333 44444 33333 44444 55555 66666 END my $olddata = <<END; Puma 77777 33333 44444 55555 Adidas 11111 11111 33333 44444 99999 88888 55555 77777 Brooks 11111 22222 33333 44444 33333 44444 55555 66666 END local $_ = $newdata . $olddata; my $label = "\t\t\t New\t\t\t\t Old\n\t\t" . " x1 y1 x2 y2\t\t x1 y1 x2 y2"; print "Matching\n$label\n"; 1 while s/^ ([a-z]+)\n \K ( (?:\d\V*\n)*?) (\d\V*)\n ( .* ^\1\n (?:\d\V*\n)*? ) \3\n / print "$1\t\t$3\t\t$3\n"; "$2$4" /gmixes; print "\n\nNot matching\n$label\n"; 1 while s/^ ([a-z]+)\n \K (\d\V*)\n ( .* ^\1\n) (\d\V*)\n / print "$1\t\t$2\t\t$4\n"; $3 /gmixes;

    Outputs :

    Matching New Old x1 y1 x2 y2 x1 y1 x2 y2 Puma 77777 33333 44444 55555 77777 33333 44444 5555 +5 Adidas 99999 88888 55555 77777 99999 88888 55555 7777 +7 Brooks 11111 22222 33333 44444 11111 22222 33333 4444 +4 Brooks 33333 44444 55555 66666 33333 44444 55555 6666 +6 Not matching New Old x1 y1 x2 y2 x1 y1 x2 y2 Adidas 22222 11111 33333 44444 11111 11111 33333 4444 +4
      Amazing! But note that if two (or more) lines in the same brand fail to match, There is no spec on how to pair them in the report.
      Bill
        I think tybalt89 needs a new category on PM for his amazing contributions.

        Something like "regex is the new assembler" or "the last maintainer committed suicide" or "NSFW" ... ;-)))

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

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
    Hi,

    first, concerning your code, I would suggest that you format your code correctly (with consistent indenting), especially for such deeply nested loops and conditionals. I am just unable to read you code as currently formatted. That would certainly help you understand what you're doing (and possibly what you're doing wrong). Granted, I could reformat it myself or run it through a prettyfier, but, sorry, I don't see why I should do that if you're too lazy to do it yourself and show us proper code. Similarly, I don't know what your data is, but using better names for your variables and hash keys might also help making sense out of your data structure.

    Second, it seems to me that your choice of data structure is quite poor (too complicated) for your needs (but that may be because you've shown us dummy data). Looking at your data, I would store your files into something simpler, such as an array of lines for each company (so, overall, a hash of arrays). Then I would compare full lines and remove (and print out) any matching line. In the end, with the data you've shown, you would end up with two lines not matching for Adidas. Now, of course, we don't know enough about your data to know how to deal with the fact that the two Adidas lines appear not to be in the same order; you would have to provide additional information on how we can figure that out.

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
    It appears to me that all you want to do is determine whether or not corresponding lines of the two input files are identical. The data structure that you have chosen to store the files makes this awkward. Is there a good reason that you must keep the existing data structure? I would recommend that you store the whole line as a single string. You would need an array of such strings for each brand name. I will create sample code if you are interested.

    UPDATE: I overlooked the problem which Laurent_R pointed out. You have not specified how to tell if the lines are out of order or what to do about it if they are.

    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1224689]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found