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