Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by Marshall (Canon)
on Jun 29, 2022 at 18:12 UTC ( [id://11145174]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found