Using 'for each' to modify a container
Ken Ray
kray at sonsothunder.com
Wed Oct 11 22:24:25 EDT 2006
On 10/11/06 5:05 PM, "Bill Marriott" <wjm at wjm.org> wrote:
>> Wow, this is very interesting to me. I just checked the documentation
> (2.6.1) on the "for each chunkType labelVariable" for of repeat, and was
> astonished to find:
>
>>>>
> Important! You cannot change the labelVariable in a statement inside the
> loop. Doing so will cause a script error. You can change the content of the
> container, but doing so will probably produce unexpected results.
> <<<
>
> Ya learn something new every day! Sounds kind of cheesy to me, though. Why
> wouldn't it be identical to
>
> repeat with i = 1 to the number of chunkType in theContainer
> put chunkType i of theContainer into labelVariable
> -- do stuff
> end repeat
Because in the example you cite above, every time through the loop, Rev has
to count the number of <chunkType> until it reaches the desired location.
Take a look at this "Script C" from this tip from a while back for more
info:
Increasing Script Performance, Part I
http://www.sonsothunder.com/devres/revolution/tips/scrp005.htm
in fact the other tips in this series are equally important:
Increasing Script Performance, Part II
http://www.sonsothunder.com/devres/revolution/tips/scrp006.htm
Increasing Script Performance, Part III
http://www.sonsothunder.com/devres/revolution/tips/scrp007.htm
> Your explanation is plausible, but if it is so then why permit ("You can
> change...") changing the contents of the container itself within the loop?
> That would seem to cause an even bigger problem.
>
> BTW, I set up a button with the following script:
>
> on mouseUp
> put empty into newContainer
> repeat for each line thisLine in fld 1
> put thisline & "***" into thisLine
> put thisLine & return after newContainer
> end repeat
> put char 1 to -2 of newContainer into fld 1
> end mouseUp
>
> Which not only didn't generate a script error but worked perfectly as
> "expected." Is the bug in the documentation, or that it doesn't generate a
> script error, or that for each doesn't let you modify the labelVariable?
It's not the "loop variable" that is problematic to change, it's the
container. So in this:
repeat for each line tLine in tData
you can manipulate tLine to your hearts content, but not tData. tData has
already been examined, parsed, and put into memory in known places so
iteration through the loop can go lightning fast... if you attempt to change
tData you throw a wrench in the works.
> And I'm *really* concerned about the second part of your report
>
>> Further, I don't reuse the name 'thisLine' in the next instance of a
>> repeat
>> for each loop in the *same handler*, but change the name to avoid
>> conflict,
>> such as 'thisline', 'thisline1', 'thisLine2'. Again, unpredictable
>> results
>> occasionally.
I personally haven't ever encountered this. I have always been able to reuse
the loop variable without problem...
Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com
More information about the use-livecode
mailing list