note
QM
<code>map</code> and <code>grep</code> are not picked up as quickly by new Perl programmers as <code>for</code>, for several reasons.
<p>
First, new programmers will have difficulty lining up multiple actions all at once. Compare these:
<code>
my @newlist = map { $_*2 } @list;
my @list;
my @newlist;
for my $item( @list)
{
push @newlist, $item * 2;
}
</code>
The first example examines all values of <code>@list</code> (one at a time of course), multiplies each value by 2, and saves all resulting values into <code>@newlist</code>.
<p>
The second example is easier for a newbie to manage. It loops through <code>@list</code>, one at a time. Each time through, <code>$item</code> will hold the active value. Multiply each value by 2, and save it in <code>@newlist</code>.
<p>
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 <code>map</code> is also easy, but it has to be done carefully to avoid munging the <code>map</code> return values.
<p>
My point is that a newbies coming from some other languages (those without <code>map</code> or <code>grep</code>) would be more comfortable with the 2nd form. Once entrenched, they are less likely to embrace <code>map</code> and <code>grep</code> without extra incentives.
<div class="pmsig"><div class="pmsig-294463">
<p>-QM<br />
--<br />
Quantum Mechanics: The dreams stuff is made of
</div></div>
501419
501419