Do you know what "map" does? If not, you might take a look at the documentation.

Let's split the code up a bit to make it easier to follow.

my $keys = '1 2 3 4 5 6'; # this splits your input data and stores the split # up elements in an array # So @key_list ends up containing # (1, 2, 3, 4, 5, 6) my @key_list = split /\s+/, $keys; # Here's where it gets interesting. # The map block is called once for every element in # the input list and returns the list that is created # by the block. # In this case each element is converted to a two-element # list, where the first element is taken from @key_list # and the second element is the value "undef". So, for # example, the first element from @key_list returns the # two-element list (1, undef). All of this little lists # are stored together in @mapped_list. So @mapped_list # will contain: # (1, undef, 2, undef, 3, undef, 4, undef, # 5, undef, 6, undef) my @mapped_list = map { $_ => undef } @key_list; # And finally we use standard list assignment to assign # our array to a hash. The even indexed elements (indexes # 0, 2, 3, etc) become the keys and the old indexed # elements (1, 3, 5, etc) become the values. # It's just like doing: # %hash = (1 => undef, 2 => undef, 3 => undef, ...) my %hash = @mapped_list;

Does that help at all?

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg


In reply to Re^3: splitting directly to hash by davorg
in thread splitting directly to hash by narashima

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.