• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Things I thought I knew, but didn't - variable scope in repeat loops
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: has <email@hidden>
  • Date: Fri, 26 Dec 2008 13:45:35 +0000

Richard Rönnbäck wrote:

For as long as I can remember I have been very cautious not to use the same variable name in nested repeat loops, or for that matter not to use the counter variable (what is the proper name for it btw?) anywhere else in the script, thinking that AppleScript would confuse what the current value of the counter is.
[...]
Then, by coincidence, I stumbled upon something that made me take a closer a look at it, and I found, to my complete surprise, that AppleScript does exactly what I want, that is, it "knows" how to scope the variable, in this case "i" to only the current repeat loop, as the log below shows:

Nope, loop variables are scoped to the local context. Example:

set x to "hello" -- binds 'x'

log x --> "hello"

repeat with x from 1 to 3 -- rebinds 'x'
	log x --> 1, 2, 3
end repeat

log x --> 3


If loop variables were scoped only to the repeat block, the final 'log' command would give 'hello', not 3.


HTH

has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net

_______________________________________________
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
  • Follow-Ups:
    • Re: Things I thought I knew, but didn't - variable scope in repeat loops
      • From: Skeeve <email@hidden>
  • Prev by Date: Re: do shell sounds
  • Next by Date: Re: on the lack of documentation (was: Re: on neophytes vs perfectionists)
  • Previous by thread: Re: Thirteen Scripters Scripting
  • Next by thread: Re: Things I thought I knew, but didn't - variable scope in repeat loops
  • Index(es):
    • Date
    • Thread