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: Matt Neuburg <email@hidden>
- Date: Sun, 28 Dec 2008 17:47:20 -0800
- Thread-topic: Things I thought I knew, but didn't - variable scope in repeat loops
On Thu, 25 Dec 2008 11:09:30 +0100, Richard R?nnb?ck
<email@hidden> said:
>Well, I have Matt's book (the first edition) with all it's explanation about
>scope, but I can't find an explanation of the variable in repeat loops
>specifically.
>
>It seems to me that repeat loops behave very much like script objects or
>handlers, with an implicit declaration of the variable as local, but I have
>never thought of repeat loops that way, so I am trying to get my head around
>what is going on.
The trick is to devise a test where you don't mislead yourself. Consider
this:
repeat with i from 1 to 2
repeat with i from 1 to 2
set i to 3
end repeat
display dialog i -- "3" both times
end repeat
display dialog i -- "3"
Thus we plainly see that the "i" of the inner loop is the "i" of the outer
loop, and that repeat loops are not a scope. If repeat loops were a scope, I
would have said so in my book! The summary on p. 286 of my book (2nd edn.)
is quite clear on this matter: at the top the loop the variable is reset to
the next value in the sequence, regardless of what you may have do to it
within the loop, but *after* the last iteration of the loop the variable is
left at whatever value you last gave it. m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
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