in reply to Combining two references

Whereas, I came up with a more pedantic solution, something like this:

use strict; use warnings; use Data::Dumper; my $data1 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', }, { 'NAME' => 'ANTHONY RD', 'DATE' => '2012-01-07', 'NUMBER' => '00003', }, ]; my $data2 = [ { 'CAR1' => '1', 'CAR2' => '2', 'CAR3' => '3', }, { 'CAR1' => '1b', 'CAR2' => '2b', 'CAR3' => '3b', }, ]; my $i=0; my @result; foreach my $row1 (@$data1) { my $record={}; while (my ($k1, $v1) = each(%$row1)) {$record->{$k1}=$v1}; my $row2=@$data2[$i]; while (my ($k2, $v2) = each(%$row2)) {$record->{$k2}=$v2}; push @result, $record; $i++; } use Data::Dumper; print Data::Dumper->Dump([\@result], ["result"]);
which gives:
$result = [ { 'CAR2' => '2', 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'CAR1' => '1', 'CAR3' => '3', 'NUMBER' => '00001' }, { 'CAR2' => '2b', 'NAME' => 'ANTHONY RD', 'DATE' => '2012-01-07', 'CAR1' => '1b', 'CAR3' => '3b', 'NUMBER' => '00003' } ];

I simply used a foreach loop to iterate over $data1, then used variable $i which is incremented within the loop to fetch the rows from $data2.   I attempted to use consistent variable-names in both cases.   I made no attempt to be clever nor efficient:   I wanted the code to be “obvious.”

One thing to keep firmly in mind, when doing things like this, is that you can easily wind up manipulating references to things, within the output that you create ... the old FORTRAN EQUIVALENCE statement ... when you might need to be making “actual copies.”   If you are moving or creating references, any change will be reflected in all references to that same thing, which might not be what you wanted nor expected.

Replies are listed 'Best First'.
Re^2: Combining two references
by Anonymous Monk on Jun 09, 2015 at 15:13 UTC
    my $row2=@$data2[$i];

    That way of accessing a single element of an array is usually discouraged unless you know what you're doing. Better is $data2->[$i].