palette has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

Consider I have two arrays one having the list of keys and
another array having list of values.

How can i create a hash with a single line using this two
arrays so that the keys array becomes the keys of the hash
and the values array becomes the values of the hash.

Thankyou

Replies are listed 'Best First'.
Re: Hash create
by Thilosophy (Curate) on Jul 04, 2005 at 12:41 UTC
    One line and a half:
    my %h; @h{@keys} = @values;
      @$_{@keys} = @values for \my %h; :)
Re: Hash create
by neniro (Priest) on Jul 04, 2005 at 12:51 UTC
    In one line:
    my %hash = map { $arr1[$_], $arr2[$_], } (0..$#arr1);
Re: Hash create
by tlm (Prior) on Jul 04, 2005 at 15:34 UTC

    In the TMTOWTDI spirit:

    use List::MoreUtils 'zip'; my %hash = zip @arr1, @arr2;

    the lowliest monk

Re: Hash create
by tphyahoo (Vicar) on Jul 05, 2005 at 11:33 UTC
    The technical term for Thilosophy's solution is a hash slice. It's important to know how these thingies work, precisely for your kind of issue. I asked a question about this a while ago that generated some discussion about this feature of perl: Is this a hash slice? if you want some additional back and forth to look at.