Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Merging Data into one array

by Anonymous Monk
on Nov 30, 2015 at 20:43 UTC ( [id://1148958]=note: print w/replies, xml ) Need Help??


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

That's a good point, I don't know if they will be all in the same order.

Replies are listed 'Best First'.
Re^3: Merging Data into one array
by stevieb (Canon) on Nov 30, 2015 at 21:01 UTC

    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://1148958]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found