Re: Page Update and discovery of looping variable manipulations...
Re: Page Update and discovery of looping variable manipulations...
- Subject: Re: Page Update and discovery of looping variable manipulations...
- From: email@hidden (Michael Sullivan)
- Date: Tue, 30 Jul 2002 12:45:20 -0400
- Organization: Society for the Incurably Pompous
thePPCGod writes:
>
>From section titled: REPEAT WITH...
>
(explains REPEAT WITH command, variations, added sections below:)
>
>
SPECIAL NOTES - the variable used to count the loop
>
iterations can NOT be changed within the loop in a way that
>
will affect the loop outcome:
>
>
REPEAT WITH GenericLoopCounter FROM 1 TO 10
>
IF (GenericLoopCounter > 5) THEN
>
SET GenericLoopCounter TO 2
>
DISPLAY DIALOG GenericLoopCounter
>
END IF
>
END REPEAT
>
>
UNEXPECTED RESULTS WILL OCCUR: The above loop, when run,
>
will display the value '2' five times, but will still loop
>
exactly 10 times. After the loop ends, the value of
>
GenericLoopCounter will be at 2.
That's really interesting. I never ran into that before. It's putting
the interior of the loop as a separate scope and passing the value of
the loop counter.
It looks like AS is behind the scenes automatically turning a standard
repeat loop:
repeat with i from 1 to 100
set a to a + i
set b to b * i
end repeat
into internal code that looks roughly like this (or some different
architecture that accomplishes the same result):
repeat with i from 1 to 100
set rval to innerLoop(i,{a: a, b: b})
set a to rval's a
set b to rval's b
end repeat
on innerLoop(i, param)
set param's a to param's a + i
set param's b to param's b * i
return params
end innerLoop
This avoids infinite loops from doing silly things you had in your
snippet, but also requires that certain kinds of coding tricks use a
while, or until loop instead (or give up performance by testing over
indexes that should be skipped). I'm actually surprised I've never run
into this given my propensity for using such coding tricks when faced
with less expressive languages. Given that they are bad style when you
don't need them, and I apparently don't need them to code in AS, this
was probably a good decision.
Michael
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.