Yes, you wrote: Both for and map iterate over lists. The code for doing so is very similar. At the end of the day you have to iterate over the incoming list in either case. My claim is that just because a map expression may be shorter in terms of characters or lines in the code, that doesn't mean that it is necessarily faster. In some cases, the opposite may even be true?

For a beginner, I would not recommend fiddling around with map expressions until the fundamentals of Perl style foreach and while loops are mastered. In my Perl coding, C style for loops are very rare because array indices are relatively rare - Actually even in my C code indices are rare because of the ubiquitous "*pointer++" to access sequential elements of an array.

The OP's problem wasn't related to writing or using a "map" expression, and I said basically that "a map expression is the not the problem or the solution". This appeared to be an algorithm question. Express the algorithm as a for (foreach) loop. Worry about map statements later.

I was taught never to use a map in void context due a big performance penalty. I hear that this performance penalty is much less now. But I still would not use map in a void context because that obscures the intent of the code. I use map for simple transformations (like the code I show below).

I have seen Tom Christiansen write some map's with maybe 15 or so lines of code inside the map. This can be quite elegant in the right situation, but few mortals such as myself encounter such situations. If a map isn't "short", I use a for loop, perhaps even creating some intermediate arrays in the process.

I guess if we get into strange things, consider the following code. It is possible to modify the loop variable within a for loop because Perl sets that up as an alias to the input list. I personally never do this in my code, preferring to create a separate output array. Not everything that is possible should be done.

This is an example where I would, from a style viewpoint use the map expression vs a for loop. @array = map{$_+5}@array; makes it clear that I have modified @array. This is not clear from the for loop. I guess that would be like "using a for loop in a void context?".

The permutations on this are endless. I hope this explains my coding philosophy adequately.

use strict; use warnings; my @array = (1,2,3,4,5); foreach my $num (@array) { $num += 5; # completely legal # but obscure } print "@array\n"; #6 7 8 9 10 @array = (1,2,3,4,5); @array = map{$_+5}@array; print "@array\n"; #6 7 8 9 10
In this case, I am not going to go into some deep dive into deparse to decide whether or not the foreach loop has more efficient code. I would use the map expression because the source code is more clear.

In reply to Re^5: How do I use the map command for this? by Marshall
in thread How do I use the map command for this? by Anonymous Monk

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.