Here's a nifty one liner to map an array of keys and an array of values to a hash.
# my %hash; # @hash{@K} = @V; # aww, two lines :( my %hash = (@K, @V)[map +($_, @K + $_), 0 .. $#K];

Replies are listed 'Best First'.
Re: Two Arrays - One Hash (one-liner)
by danger (Priest) on Jul 06, 2001 at 21:23 UTC

    While yours is cute, there is a more obvious approach that is both shorter and (according to a quickie benchmark) slightly more efficient:

    my %hash = map{$K[$_] => $V[$_]} 0 .. $#K;

    However, the two line slice version is actually quite a bit shorter to type and (again, according to a quickie benchmark) more than twice as efficient as either of the one-liner versions.

    There is, of course, a semantic difference between the slice and one-line approaches --- namely, the one-liners set the entire hash to the key/values given, whereas the slice version retains any additional key/values that might have already been present in the hash. Not relevant in the snippets given because we are declaring and immediately assigning to the hash, but if one is using a hash and wants to assign to it a completely new set of key/value pairs, one will need to undef or assign an empty list to the hash prior to using the slice version.