Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

One way (update: see the Perl Data Structures Cookbook):

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', } ]; foreach my $hashref (@$data1) { %$hashref = (%$hashref, %{ $data2->[0] }); } print Dumper $data1; __END__ $VAR1 = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'CODE' => 'X11', 'NUMBER' => '00001' }, { 'NAME' => 'ANTHONY RD', 'DATE' => '2012-01-07', 'CODE' => 'X11', 'NUMBER' => '00003' }, { 'NAME' => 'RUTH RD', 'DATE' => '2018-01-07', 'CODE' => 'X11', 'NUMBER' => '00023' } ];

Update 1: Note that using this method, if there is a key in the hash referent of the single element of the  $data2 array referent that is the same as one in a hash referent of any  $data1 array referent, the value of the former will silently overwrite the value of the latter. (Update: For instance, see what happens if  $data2 happens to be
    my $data2 = [ { 'CODE' => 'X11',  'NAME' => 'JONES', } ];
instead. (Update: This exact problem is discussed more clearly in the FAQ referenced by BillKSmith here.))

Update 2: Here's a testing framework for playing around with other approaches. See Test::More, Test::NoWarnings.

c:\@Work\Perl\monks>perl use strict; use warnings; use Test::More 'no_plan'; use Test::NoWarnings; 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', } ]; foreach my $hashref (@$data1) { %$hashref = (%$hashref, %{ $data2->[0] }); } my $expected = [ { 'NAME' => 'PAUL DY', 'DATE' => '2009-05-05', 'NUMBER' => '00001', 'CODE' => 'X11', }, { 'NAME' => 'ANTHONY RD', 'DATE' => '2012-01-07', 'NUMBER' => '00003', 'CODE' => 'X11', }, { 'NAME' => 'RUTH RD', 'DATE' => '2018-01-07', 'NUMBER' => '00023', 'CODE' => 'X11', }, ]; is_deeply $data1, $expected, 'success!'; done_testing; __END__ ok 1 - success! 1..1 ok 2 - no warnings 1..2


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


In reply to Re^3: Insert into element into arrays ref (updated) by AnomalousMonk
in thread Insert into element into arrays ref by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-23 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found