Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: question for perl book & magazine authors

by QM (Parson)
on Oct 21, 2005 at 14:31 UTC ( #502010=note: print w/replies, xml ) Need Help??

in reply to question for perl book & magazine authors

map and grep are not picked up as quickly by new Perl programmers as for, for several reasons.

First, new programmers will have difficulty lining up multiple actions all at once. Compare these:

my @newlist = map { $_*2 } @list; my @list; my @newlist; for my $item( @list) { push @newlist, $item * 2; }
The first example examines all values of @list (one at a time of course), multiplies each value by 2, and saves all resulting values into @newlist.

The second example is easier for a newbie to manage. It loops through @list, one at a time. Each time through, $item will hold the active value. Multiply each value by 2, and save it in @newlist.

Second, it's easier to debug the 2nd example. There are several places to breakpoint or print out intermediate results to check that the code behaves as expected. Printing out intermediate results in a map is also easy, but it has to be done carefully to avoid munging the map return values.

My point is that a newbies coming from some other languages (those without map or grep) would be more comfortable with the 2nd form. Once entrenched, they are less likely to embrace map and grep without extra incentives.

Quantum Mechanics: The dreams stuff is made of

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://502010]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-06-18 05:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (108 votes). Check out past polls.