The thing you're looking for is called a hash slice.

#!/usr/bin/perl -wl use strict; # Explicit lists of keys and values my %foo; # Note the '@' in front of the hash name! @foo{1..26} = "a".."z"; print $foo{10}; # Prints 'j' # You can use hash slices to return a list, too: print @foo{qw/8 5 12 12 15/}; # Prints 'hello' # You can use it to associate lists: my @employee_names = qw/Joe Jane Jim Jessie/; my @employee_ids = qw/1234 1235 1236 1237/; my %empl_lookup; @empl_lookup{@employee_ids} = @employee_names; # Now, you can look up employee names by ID: print $empl_lookup{1236}; # Hi there, Jim!

It's a very useful method in Perl, and worth knowing well.

Update: Whoops - I just noticed that I missed a couple of bytes in the question, and thus answered something other than what was asked.

#!/usr/bin/perl -wl use strict; my (@a, @b, $foo); @a = qw/key1 key2/; @b = qw/val1 val2/; $foo = { map { $a[$_] => $b[$_] } 0..$#a }; print $foo->{key1}; # Prints 'val1'

The above is a bit more like it. :)


--
"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

In reply to Re: Assigning values to undef hash reference keys two or more at a time by oko1
in thread Assigning values to undef hash reference keys two or more at a time by gctaylor1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.