http://www.perlmonks.org?node_id=11104880


in reply to Re: Insert into element into arrays ref
in thread Insert into element into arrays ref

for (my($k,$v)=each %{$data2->[0]}){ $_->{$k} = $v for @$data1; }

That works for a single key/value element in $data2->[0], but given that it can work for only a single element, why not use the ocularly even easier

my ($k,$v) = %{$data2->[0]}; $_->{$k} = $v for @$data1;

c:\@Work\Perl\monks>perl 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', }, { 'NAME' => 'RUTH RD', 'DATE' => '2018-01-07', 'NUMBER' => '00023', }, ]; my $data2 = [ { 'CODE' => 'X11', 'X' => 'Y', } ]; for (my($k,$v)=each %{$data2->[0]}){ $_->{$k} = $v for @$data1; } print Dumper $data1; __END__ $VAR1 = [ { 'NAME' => 'PAUL DY', 'X' => 'Y', 'DATE' => '2009-05-05', 'NUMBER' => '00001' }, { 'NAME' => 'ANTHONY RD', 'X' => 'Y', 'DATE' => '2012-01-07', 'NUMBER' => '00003' }, { 'NAME' => 'RUTH RD', 'X' => 'Y', 'DATE' => '2018-01-07', 'NUMBER' => '00023' } ];


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: Insert into element into arrays ref
by aartist (Monk) on Aug 23, 2019 at 11:01 UTC
    for (my($k,$v)=each %{$data2->[0]}){

    You may need to use while in place of for to get all the value in %{$data2->[0]}