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: Richard Rönnbäck <email@hidden>
- Date: Sat, 27 Dec 2008 10:39:16 +0100
- Thread-topic: Things I thought I knew, but didn't - variable scope in repeat loops
Thanks Mark,
You got me right, I was thinking of other languages where one increments the
counter.
It turns out that my old habit of using unique loop variables is sound after
all, as this example shows:
set i to "hi, mom!"
repeat with i from 1 to 10
say i
end repeat
say i
So while setting i to something else does not affect the loop, the loop
affects the variable i.
> Från: "Mark J. Reed" <email@hidden>
> Datum: Fri, 26 Dec 2008 19:10:14 -0500
> Till: has <email@hidden>
> Kopia: <email@hidden>
> Ämne: Re: Things I thought I knew, but didn't - variable scope in repeat loops
>
> 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:
> edband.net
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
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