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: Andy Wylie <email@hidden>
- Date: Wed, 31 Jul 2002 14:46:18 +1200
on 7/31/02 4:45 AM +1200: Michael Sullivan wrote:
>
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.
>
>
if the urge should ever strike...
set cnt to 1
set x to {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
looper(cnt, x)
on looper(cnt, x)
display dialog (x's item cnt)
if ((x's item cnt) > 5) then
set cnt to 2
display dialog cnt
looper(cnt, x)
else
set cnt to cnt + 1
looper(cnt, x)
end if
end looper
_____________________________ Andy
_______________________________________________
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.