Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: How do I use the map command for this?

by Marshall (Canon)
on Jun 22, 2022 at 22:02 UTC ( [id://11144962]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How do I use the map command for this?
in thread How do I use the map command for this?

Would certainly enjoy being educated. I probably should have said "similar". A map is a looping statement.
  • Comment on Re^3: How do I use the map command for this?

Replies are listed 'Best First'.
Re^4: How do I use the map command for this?
by Fletch (Bishop) on Jun 23, 2022 at 21:46 UTC

    Both for and map iterate over lists, but the latter has the extra purpose that it returns a transformed copy of the list. There's been optimizations that make map in void context efficient (or at least not as inefficicent as it used to be) so you could use  map { do_something( $_ ) @list in place of for (@list) { do_something( $_ ) }, but it's clearer/cleaner to use for for iteration and map for transformation and not muddle the two.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      ... you could use map { do_something( $_ ) @list in place of for (@list) { do_something( $_ ) } ...

      And likewise grep for a really confusing maintenance session! (grep has the same optimization in void context.)


      Give a man a fish:  <%-{-{-{-<

      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.
Re^4: How do I use the map command for this?
by LanX (Saint) on Jun 23, 2022 at 20:10 UTC
    > A map is a looping statement.

    map is a built-in function used as expression, not a statement

    you are right that most uses can be transformed, but there are differences in syntax and implementation

    as an example, for - like most non-simple statements - doesn't return values.

    edit
    see also Re: why does Perl eval have a strange terminator?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re^4: How do I use the map command for this?
by Anonymous Monk on Jun 23, 2022 at 04:52 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144962]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found