If you want to merge 2 hashes in Perl, you can of course do it manually, e.g. like this:
%a = ('foo'=>123, 'bar'=>456);
%b = ('baz'=>789, 'zak'=>100);
while ( ($k,$v) = each(%a) ) { $c{$k} = $v; }
while ( ($k,$v) = each(%b) ) { $c{$k} = $v; }
or, a little more elegant:
[...]
%c = ( %a, %b );
(see the
Perl Cookbook, Chapter 5)
This method of course fails for more complicated situations, e.g.
%a = (
names => [ 'Huey', 'Dewey', 'Louie' ],
age => [12, 13, 14]
);
%b = (
names => [ 'Donald' ],
age => [96]
);
Luckily, there's a useful little Perl module on CPAN to handle this:
Hash::Merge
Using it is quite simple:
%c = %{ merge( %a, %b ) };