MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: foreach loop


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


  • Prev by Date: Re: foreach loop
  • Next by Date: Re: Functions with optional parameters
  • Previous by thread: Re: foreach loop
  • Next by thread: Re: foreach loop