repeat with each woord currentWord in myString

Ken Ray kray at sonsothunder.com
Sat Mar 20 16:38:35 EST 2004


> repeat for each word someWord in someVariable
>     doSomething someWord
> end repeat

One thing to note that is VERY IMPORTANT when working with the "repeat for
each" concept, and that is that you cannot change 'someVariable' inside the
loop or things get really screwy as the engine loses track of which chunk to
process next. 

For example, suppose you wanted to iterate through a multi-line container,
and remove any line that starts with "T" from the container.

You might think to do this:

put 1 into tCounter
repeat for each line tLine in tContainer
  if char 1 of tLine = "T" then
    delete line tCounter of tContainer
  end if
  add 1 to tCounter
end repeat

But this is BAD since you're changing tContainer in the middle of the loop.
The proper way to do this would be to build another string at the same time:

put "" into tNewContainer
repeat for each line tLine in tContainer
  if char 1 of tLine <> "T" then
    put tLine & CR after tNewContainer
  end if
end repeat
delete last char of tNewContainer

This may look like a lot of lines and a bit odd, but it is *really* fast.
There's more on this at my site by the venerable Wil Dijkstra on increasing
script performance:

"Increasing Script Performance, Part I"
http://www.sonsothunder.com/devres/revolution/revolution.htm?_scrp005

"Increasing Script Performance, Part II"
http://www.sonsothunder.com/devres/revolution/revolution.htm?_scrp006

"Increasing Script Performance, Part III"
http://www.sonsothunder.com/devres/revolution/revolution.htm?_scrp007

Enjoy!

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/




More information about the use-livecode mailing list