in reply to Merging references

my $dst = Sub1(); my $src = Sub2(); foreach my $key1 (keys %$src) { my $inner = $src->{$key1}; foreach my $key2 (keys %$inner) { my $val = $inner->{$key2}; $dst->{$key1}{$key2} = $val; } }

Replies are listed 'Best First'.
Re^2: Merging references
by ferreira (Chaplain) on Dec 28, 2006 at 09:57 UTC
    Or you could use a slice instead of the inner loop. The following piece is a rewrite, that is easily extended to more than merging the result of two subs:
    my $part1 = Sub1(); my $part2 = Sub2(); my $ans = {}; # where the final answer will live foreach my $part ($part1, $part2) { # loop over parts foreach my $k (keys %$part) { my $inner = $part->{$k}; @{$ans->{$k}}{keys %$inner} = values %$inner; } } use Data::Dump qw(dump); print dump $ans; # that's what you got

    Obviously, this merge only works at two-level hashes.