Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Merging Data into one array

by stevieb (Canon)
on Nov 30, 2015 at 21:01 UTC ( [id://1148963]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Merging Data into one array
in thread Merging Data into one array

List::MoreUtils to the rescue here... we'll generate a new array with every ACC in $data2 (ordered), then we'll iterate over $data1, using first_index() to search for its ACC in the new array created above. That'll return to $index the element location from the new array, which we then use to extract the full hash from the proper location (index) of the full $data2. Make sense? ;)

use List::MoreUtils qw(first_index); my @all; my @data2_acc_list; push @data2_acc_list, $data2->[$_]{ACC} for 0..$#$data2; for (0..$#$data1){ my $acc = $data1->[$_]{ACC}; my $index = first_index { /$acc/ } @data2_acc_list; if ($index >= 0){ push @all, { %{ $data1->[$_] }, %{ $data2->[$index] } }; } } print Dumper \@all;

Note that if $data2 has matching ACC values, things will probably be squirly, as only the first one will be hit. I also had to read the docs on first_index()... it returns -1 if the element can't be found, and I was checking for truth, not explicitly for zero or greater.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-20 15:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found