Dan Sopher wrote:

Aside from the syntax, is there a difference in the way 'map' and
'foreach' process?

Hi Dan

Internally they're very similar, but you shouldn't be thinking like that.

As Randal said, foreach is a statement - a language construct like 'if',
'while', 'else' and so on - while map is an expression - more specifically
a function.
Use foreach if you want to execute a block of Perl code for every element
in a list.

Use map to implement a /mapping/ between two lists. It takes an input list
and a statement or a block specifying a transformation, and (in list
context) returns the list with that transformation applied to each element.
Conceptually there is no loop - the entire list is transformed at once, and
a scalar expression like

 $a = 2 * $b

corresponds exactly to the list expression

 @a = map 2 * $_, @b

HTH,

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to