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' };

In reply to Re: Add array ref to another array ref by stevieb
in thread Add array ref to another array 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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.