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.
|