Re: Things I thought I knew, but didn't - variable scope in repeat loops
Re: Things I thought I knew, but didn't - variable scope in repeat loops
- Subject: Re: Things I thought I knew, but didn't - variable scope in repeat loops
- From: "Mark J. Reed" <email@hidden>
- Date: Fri, 26 Dec 2008 19:10:14 -0500
I think the OP might have been confused by the fact that the loop
control variable is stateless in Applescript. That is, when you do
this:
repeat with i from 1 to 10
Applescript builds a list of values (or an iterator/generator) and
sets i to the next value at the top of the loop; it doesn't look at
the current value of i to determine what the next value of i should
be. So you can do anything you want to i inside the loop and it
won't affect the loop control logic:
repeat with i from 1 to 10
set i to "hi, mom!"
say i
end repeat
Some languages, on the other hand, implement counting loops by
incrementing the value of the counter (or, as in the case of C, leave
it up to the programmer, who usually chooses to use an increment
step). In such a language, this set of nested loops would only
execute the outer loop once:
for i=1 to 3
for i=1 to 3
print i
rof
rof
Other languages, like Pascal, treat loop control variables specially
and won't even compile an attempt to use the same variable; BASIC uses
the variable to pair the top and bottom of the loop, so hitting the
bottom of the outermost loop will generate a runtime error.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden