Clearing local variables
Mark Waddingham
mark at livecode.com
Fri Apr 29 12:06:57 EDT 2016
On 2016-04-29 17:24, Richard Gaskin wrote:
> In the rare cases where we might want to modify the string being
> traversed, does the "repeat with i = 1 to..." form still allow that?
Yes - you can still modify the string being traversed with repeat for
each, but the modifications won't actually affect what is being iterated
over.
repeat for each line tLine in tMyLines
... mutate tMyLines ...
end repeat
Can be seen as being:
local __tMyLines
put tMyLines into __tMyLines
repeat for each line tLine in __tMyLines
... mutate tMyLines ...
end repeat
And due to the copy-on-write semantics in 7+, you don't actually pay for
the 'invisible' temporary copy unless you actually do a modification.
In the case of 'repeat with' - the expressions involved are all
evaluated before the loop starts:
repeat with x = 1 to the number of lines in tMyLines
... mutate tMyLines ...
end repeat
Can be seen as doing:
local __start, __end
put 1 into __start
put the number of lines in tMyLines into __end
repeat with x = __start to __end
... mutate tMyLines ...
end repeat
As you can see, tMyLines (or even a copy of it) doesn't actually figure
in the control part of the loop after it starts going.
Warmest Regards,
Mark.
--
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps
More information about the use-livecode
mailing list