in reply to Re: Hash merging!
in thread Hash merging!

Which itself can be reduced to %a = (%a, %b);. Granted if you wanted to preserve the elements of either hash, you would have to use the above method. I presume the behavior is undefined, but on my machine:

%a = (%a, %b); # common elements contain %b's values %a = (%b, %a); # common elements contain %a's values

Replies are listed 'Best First'.
Re^3: Hash merging!
by ambrus (Abbot) on Apr 26, 2005 at 08:57 UTC

    Yes, it is simpler, but it seems that @a{keys %b} = values %b; is consistently faster. A quick mesurement shows that

    perl -we 'use Time::HiRes "time"; %a = map rand, 1 .. (1<<18); %b = ma +p rand, 1 .. (1<<18); warn($t = time()); %a = (%a, %b); warn time() - + $t; warn 0+keys(%a);'
    runs in 2.8s, while
    perl -we 'use Time::HiRes "time"; %a = map rand, 1 .. (1<<18); %b = ma +p rand, 1 .. (1<<18); warn($t = time()); @a{keys %b} = values %b; war +n time() - $t; warn 0+keys(%a);'
    in 1.0s.