Re: foreach loop
- To: mathgroup at smc.vnet.net
- Subject: [mg51966] Re: foreach loop
- From: D Herring <dherring at at.uiuc.dot.edu>
- Date: Fri, 5 Nov 2004 02:20:07 -0500 (EST)
- References: <cmckcr$i2d$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
symbio wrote:
> Is there a foreach loop statement in Mathematica? I need to loop through a
> list of words, rather than numbers. I need the equivalent of Do[expr,
> {n,0,10,1}], except I need to step through a list like so: foreach loop
> [expr, {fall, winter, spring, summer}]. Can anyone help?
>
One method:
seasons={fall, winter, spring, summer}
Do[expr[seasons[[i]]], {i, Length[seasons]}]
Another:
Map[expr, {fall, winter, spring, summer}]
Equivalently:
expr /@ {fall, winter, spring, summer}
I'm sure there are numerous other creative ways of doing things.
Later,
Daniel