Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Add array ref to another array ref

by stevieb (Canon)
on Jul 15, 2019 at 18:57 UTC ( [id://11102881]=note: print w/replies, xml ) Need Help??


in reply to Add array ref to another array ref

You're working on hashes (hash references, actually), not arrays (array references). There's a significant difference, and push() is used on arrays, which is why you got a collection of hash references within a new array reference. Note that hashes are sometimes referred to as "associative array", which is not the same thing as a normal array.

Here's two ways to do what you want. I created a new hash ref ($hash_c) so I didn't clobber $hash_b before doing the second example. The result is the same though. The first example uses map(), and if both $hash_a and $hash_b both have a key with the same name, the one in $hash_b will be overwritten with $hash_a's value. The second example does a key/value add, but if $hash_b already has a key in $hash_a, we'll just skip it and keep $hash_b's value:

use warnings; use strict; use Data::Dumper; my $data_a = { 'fullname' => 'Ms Mary Lou', 'first' => 'Mary', 'last' => 'Lou', }; my $data_b = { 'house' => 'main', 'number' => '0123', 'area' => 'north', 'code' => 'zip', }; my $data_c; %{ $data_c } = map { $_ => $data_a->{$_} } keys %$data_b, keys %$data_ +a; for my $k (keys %$data_a){ $data_b->{$k} = $data_a->{$k} if ! exists $data_b->{$k}; } print Dumper $data_c; print Dumper $data_b;

Output:

$VAR1 = { 'house' => undef, 'code' => undef, 'fullname' => 'Ms Mary Lou', 'last' => 'Lou', 'area' => undef, 'first' => 'Mary', 'number' => undef }; $VAR1 = { 'first' => 'Mary', 'number' => '0123', 'house' => 'main', 'code' => 'zip', 'fullname' => 'Ms Mary Lou', 'last' => 'Lou', 'area' => 'north' };

Replies are listed 'Best First'.
Re^2: Add array ref to another array ref
by Fletch (Bishop) on Jul 15, 2019 at 19:01 UTC

    Trying to push onto a hashref will produce an error so my guess is that $data_b is coming back from wherever as an AoH to begin with.

    DB<1> $a = { qw/ foo bar/ } DB<2> $b = { qw/baz quux/ } DB<3> x +{ %$a, %$b } 0 HASH(0x7ff1242138f0) 'baz' => 'quux' 'foo' => 'bar' DB<4> x push @{ $b }, $a Not an ARRAY reference at (eval 17)[/System/Library/Perl/5.18/perl5db. +pl:732] line 2.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Add array ref to another array ref
by Anonymous Monk on Jul 15, 2019 at 19:21 UTC
    I am getting confused with the data types.
    I did a data dumper on $data_b and got this:
    $VAR1 = [ { 'house' => 'main', 'number' => '0123', 'area' => 'north', 'code' => 'zip', }; ]

    And on $data_a was this:
    $VAR1 = { 'fullname' => 'Ms Mary Lou', 'first' => 'Mary', 'last' => 'Lou', };

      When you see the square brackets [], that's an arrayref (a reference to an array). When you see braces {}, that's a hashref (a reference to a hash).

      So the first VAR1 that you see is an arrayref with a single element, which is a hashref. The second VAR1 is just a hashref.

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      Please see this, especially the Update.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found