repeat for each....

Klaus Major klaus at major-k.de
Mon Aug 25 08:15:01 EDT 2003


Hi Mark

> This is driving me barmy...

Again i learned a useful english word ;-)

> can any kind soul here explain why this works:
>
> on tryLoopWith
>   put "a,b,c,d,e,f" into charList
>   repeat with n = 1 to the number of items in charList
>     put item n of charList into line n of newCharList
>   end repeat
>   put newCharList && "with"
> end tryLoopWith
>
> but this throws up a 'bad chunk expression" :
>
> on tryLoopFor
>   put "a,b,c,d,e,f" into charList
>   repeat for each item n in charList
>     put item n of charList into line n of newCharList
>   end repeat
>   put newCharList && "for"
> end tryLoopFor

"repeat for each..." is a read-only thingie... ;-)
Means that "n" in this case will not hold the number of the current 
line as in "repeat with...",
but the CONTENT of "the current line of charlist"...

So there is no "line n of newCharList".
(The engines sees: line "a" of newCharList...)
...and there is no such thing as "item n of charlist".
(And here: item "a" of charlist...)

That's why you get an error...

Try to create an extra counter for this purpose.
Even with a counter the "repeat for each..." loop is still insanely 
fast :-)

on tryLoopFor
   put "a,b,c,d,e,f" into charList
   put 1 into mycounter
   repeat for each item n in charList
     put n into line mycounter of newCharList
     ### just pure n :-)
     add 1 to mycounter
   end repeat
   put newCharList && "for"
end tryLoopFor

> I haven't yet been able to make the 'repeat for each...' structure 
> work at all, not even once,
> in any circumstance, in a year of trying it from time to time.

It is a bit tricky, indeed...

> I can find nothing in the electronic docs to help me. I am mystified.

I am sure we will be able to guide you into the right direction ;-)

> TIA,  Mark Smith

Hope that helps...


Regards

Klaus Major
klaus at major-k.de
www.major-k.de




More information about the use-livecode mailing list