MathGroup Archive 2004

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

Search the Archive

Re: foreach loop

  • To: mathgroup at
  • Subject: [mg51962] Re: foreach loop
  • From: Peter Pein <petsie at>
  • Date: Fri, 5 Nov 2004 02:19:54 -0500 (EST)
  • References: <cmckcr$i2d$>
  • Sender: owner-wri-mathgroup at

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? 
  I would try:

words = ToString /@ {fall, winter, spring, summer};
Module[{test, w = words}, While[w != {},
    test = First[w]; w = Rest[w];
     Print[StringJoin[Reverse[Characters[test]]]]; ]]

where the Module[] protects global variables with names "test" and "w".

P.S.: again, spring is most amusing :-)

Peter Pein

  • Prev by Date: Re: Functions with optional parameters
  • Next by Date: Re: Counting Runs
  • Previous by thread: Re: foreach loop
  • Next by thread: any tool/package to solve markov chain?