in reply to Merging Data into one array

You don't specify if all your hashes will always be in the same order in each array ref. This example code below believes that they will be (which was kind of apparent in your code, but one never knows):

for (0..$#$data1){ if ($data1->[$_]{ACC} eq $data2->[$_]{ACC}){ push @all, { %{ $data1->[$_] }, %{ $data2->[$_] } }; } }

Replies are listed 'Best First'.
Re^2: Merging Data into one array
by Anonymous Monk on Nov 30, 2015 at 20:43 UTC
    That's a good point, I don't know if they will be all in the same order.

      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.