in reply to Hash merging!

I think it's just an overcomplicated version for this:

@a{keys %b} = values %b;

(Update: changed $ silgil to @.)

Replies are listed 'Best First'.
Re^2: Hash merging!
by eibwen (Friar) on Apr 26, 2005 at 08:40 UTC

    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

      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.