<?xml version="1.0" encoding="windows-1252"?>
<node id="997338" title="Re: Efficiency of map vs. more verbose basic/fundamental code" created="2012-10-04 18:47:34" updated="2012-10-04 18:47:34">
<type id="11">
note</type>
<author id="176576">
eyepopslikeamosquito</author>
<data>
<field name="doctext">
&lt;P&gt;
The later edition of
&lt;a href="http://www.effectiveperlprogramming.com/"&gt;Effective Perl Programming&lt;/a&gt; has item 20, &lt;I&gt;"Use foreach, map and grep as appropriate"&lt;/I&gt;,
which gives an excellent summary of when to use &lt;C&gt;foreach&lt;/C&gt;, &lt;C&gt;map&lt;/C&gt; and &lt;C&gt;grep&lt;/C&gt;:
 &lt;ul&gt;
  &lt;li&gt; Use &lt;C&gt;foreach&lt;/C&gt; to iterate read-only over each element of a list
  &lt;li&gt; Use &lt;C&gt;map&lt;/C&gt; to create a list based on the contents of another list
  &lt;li&gt; Use &lt;C&gt;foreach&lt;/C&gt; to modify elements of a list
  &lt;li&gt; Use &lt;C&gt;grep&lt;/C&gt; to select elements in a list
 &lt;/ul&gt;
&lt;/P&gt;

&lt;P&gt;
Hall, McAdams and foy further caution against modifying a list via &lt;C&gt;map&lt;/C&gt;:
&lt;blockquote&gt;
"For efficiency, &lt;C&gt;$_&lt;/C&gt; is actually an alias for the current element in the iteration. If you modify &lt;C&gt;$_&lt;/C&gt; within the transform expression of a &lt;C&gt;map&lt;/C&gt;, you modify the input data. This is generally considered to be bad style, and -- who knows? -- you may even wind up confusing yourself this way. If you want to modify the contents of a list, use &lt;C&gt;foreach&lt;/C&gt;."
&lt;/blockquote&gt;
&lt;/P&gt;

&lt;P&gt;
I'm pretty sure the earlier edition you are reading has a similar item.
If you read this item, you should not be confused about which one to use.
I suggest you focus on clarity and only bother benchmarking if
performance is really critical.
&lt;/P&gt;
</field>
<field name="root_node">
997326</field>
<field name="parent_node">
997326</field>
</data>
</node>
