perlfaq nodetype
faq_monk
<P>
To call a function on each element in an array, and collect the results,
use:
<P>
<PRE> @results = map { my_func($_) } @array;
</PRE>
<P>
For example:
<P>
<PRE> @triple = map { 3 * $_ } @single;
</PRE>
<P>
To call a function on each element of an array, but ignore the results:
<P>
<PRE> foreach $iterator (@array) {
&my_func($iterator);
}
</PRE>
<P>
To call a function on each integer in a (small) range, you <STRONG>can</STRONG> use:
<P>
<PRE> @results = map { &my_func($_) } (5 .. 25);
</PRE>
<P>
but you should be aware that the <CODE>..</CODE> operator creates an array of all integers in the range. This can take a lot
of memory for large ranges. Instead use:
<P>
<PRE> @results = ();
for ($i=5; $i < 500_005; $i++) {
push(@results, &my_func($i));
}
</PRE>
<P>