"..an "inside-out" version of foreach."
That's a handy way of looking at map.

I'm not so sure about:

DO_SOMETHING_WITH-$_
In both cases you would be changing the array. Indeed it would only work with an array (see the comment below). This is clearly your intention with the map as you've used it in void context, i.e. although map returns a list you're just throwing it away.

It may be better to say map is useful if you want to create an array based on another array. I think: "Do something with a $_, but don't change it" would be better/safer. :-)

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array = qw{a b c}; my @new_array = map{my $letter = $_; ++$letter} @array; print "before: @array\n"; print "after: @new_array\n"; # with some extra white space # it looks even more 'inside-out' :-) my @another = map{ my_func() } @new_array; print "another: @another\n"; print "original \@array: @array\n"; sub my_func { my $letter = $_; $letter++; return $letter; } exit; # DO_SOMETHING_WITH-$_ # doesn't work on a list for (qw{a b c}){ # this line generates: # "Modification of a read-only value attempted at..." ++$_; } # so does this map {++$_} qw{a b c};
output:
---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl before: a b c after: b c d another: c d e original @array: a b c > Terminated with exit code 0.
updated: corrected my_func sub.

In reply to Re^2: Converting a HoH to an AoA by wfsp
in thread Converting a HoH to an AoA by madbombX

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.