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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |