While understanding what map{} is supposed to do, I seem to be consistently unable to use it correctly... which suggests that, on some level I really don't know what it actually does.

As I understand it, map{} applies some function (defined within the BLOCK) to each element of an array and the result is passed to the left.

So, you could, for instance, write a for loop without using a for simply by writing:

my @array = (1, 2, 3, 4, 5); my @adjusted_array = map { $_ * 10 } @array; print (join ',', @adjusted_array);

But what about more complicated statements?

I make it about as far as loading a hash from an array that I've split before it all starts to look like a monkey hitting the keys largely at random.

my @array = ('one-1', 'two-2', 'three-3', 'four-4', 'five-5'); my %hash = map { split /-/ } @array; print (join ',', keys %hash);

Why, for instance, could I not work this through even though it's clearly a mapping situation?

if ($access) { foreach (@{$access}) { $set{$_->[0]} = $_->[1]; } }

I guess what I'm seeking is the wisdom to understand how map operates within the BLOCK since most mentions of the map{} function are either cryptic or basic and I fall somewhere in between.

TIA


In reply to Back to Remedial Perl for Me: map{} function by jreades

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.