Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Insert into element into arrays ref

by AnomalousMonk (Bishop)
on Aug 23, 2019 at 05:04 UTC ( #11104880=note: print w/replies, xml ) Need Help??


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]}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11104880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2019-09-17 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (201 votes). Check out past polls.

    Notices?