in reply to Merging Data into one array

Here's a solution with just map and grep:

#!/usr/bin/env perl use strict; use warnings; my $data1 = [ { 'NAME' => '1PAUL DY', 'DATE' => '12009-05-05', 'NUMBER' => '1000 +01', 'ACC' => '1A1A', }, { 'NAME' => '2PAUL DY', 'DATE' => '2011-01-05', 'NUMBER' => '20033 +1', 'ACC' => '2A3B', }, { 'NAME' => '4PAUL DY', 'DATE' => '42011-01-05', 'NUMBER' => '4200 +331', 'ACC' => '6A4B', }, ]; my $data2 = [ { 'EXT1' => '1b', 'EXT2' => '12b', 'EXT3' => '13b', 'EXT4' => '14 +d', 'ACC' => '1A1A', }, { 'EXT1' => '2b', 'EXT2' => '2b', 'EXT3' => '2b', 'EXT4' => '2d', + 'ACC' => '2A3B', }, { 'EXT1X' => '4b', 'EXT2X' => '4b', 'EXT3X' => '4b', 'EXT4X' => ' +4d', 'ACC' => '4A4B', }, ]; my @merged = map { { %{$data1->[$_]}, %{$data2->[$_]} } } grep { $data1->[$_]{ACC} eq $data2->[$_]{ACC} } 0 .. $#$data1; # Check results use Data::Dump; dd \@merged;

Output:

$ pm_1148951_merge_arrays.pl [ { ACC => "1A1A", DATE => "12009-05-05", EXT1 => "1b", EXT2 => "12b", EXT3 => "13b", EXT4 => "14d", NAME => "1PAUL DY", NUMBER => 100001, }, { ACC => "2A3B", DATE => "2011-01-05", EXT1 => "2b", EXT2 => "2b", EXT3 => "2b", EXT4 => "2d", NAME => "2PAUL DY", NUMBER => 200331, }, ]

— Ken

Replies are listed 'Best First'.
Re^2: Merging Data into one array
by SimonPratt (Friar) on Dec 03, 2015 at 15:05 UTC

    This is an exceptionally fragile solution. It assumes not only that each array contains the same number of records, but that every related record is in the same place in each array.

      I make a point of clearly stating any assumptions I make. I have made no assumptions here.

      The OP wrote "I am trying to merge these two arrays ...". My solution merges the two arrays provided by the OP.

      — Ken