the way to do what you want is as follows:
my @keynames = qw/first second third fourth/; my %hash = map { $_, 0 } @keynames;

you can initialize a hash with values from an array as follows (this is called a hash slice):

my @keynames = qw/first second third fourth/; my @squares = map { $_ ** 2 } (1 .. 4); my %hash; @hash{@keynames} = @squares;

also, in your example, i don't think the hash is totally empty. it should have a single undefined element in $hash{0}. i believe this is because the map variant you were using was treating the final value from your list (i.e. '0') as the map code and overwriting that element in the hash with undef for every element in @keynames.


In reply to Re: Using map to interlace an array by mdillon
in thread Using map to interlace an array by athomason

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.