• 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: How to do a goto with Applescript?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to do a goto with Applescript?


  • Subject: Re: How to do a goto with Applescript?
  • From: Chris Page <email@hidden>
  • Date: Wed, 12 Jan 2011 21:25:27 -0800

On Jan 10, 2011, at 8:50 AM, Mark J. Reed wrote:

On Mon, Jan 3, 2011 at 11:27 AM, Skeeve <email@hidden> wrote:
Am 03.01.11 15:45, schrieb David Crowe:

There's always a better way to write the code than use a goto.
Some well known scientists and programmers disagree like  Linus Torvalds, Steve McConnell or Donald Knuth http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf

Goto is not inherently evil, and Dijkstra may have overstated his case, but the point stands.

Knuth has an example of a case where using a goto is more efficient (and possibly simpler to read), but then goes on to show how that idiom could be explicitly supported with a new language structure that doesn't require a goto. goto per se is undesirable because it is arbitrary control flow, but nearly all the cases where it seems necessary use it in a constrained manner that could be encapsulated into a language structure that does not require the arbitrary power of goto.

The occasional goto can be quite liberating, saving you from having to exit out of deeply nested loops or conditionals or whatnot.

Since I haven't seen people suggest this, I'll take this opportunity to mention that try blocks can be used to provide arbitrarily nested exits; you can even use them to exit from inside functions called directly or indirectly by the nested code:

-- a collection of stuff upon which to operate
set stuff to {{"AA", "AB", "AC"}, {"BA", "BB", "BC"}, {"CA", "CB", "CC"}, {"DA", "DX", "DC"}}

-- nested loops with arbitrary levels of exiting
set tests to 0
set subMatches to 0
try
repeat with theItem in stuff
try
repeat with theSubItem in theItem
set tests to tests + 1
if theSubItem contains "A" then
set subMatches to subMatches + 1
else if theSubItem contains "B" then
error number 2 -- exit interior loop
else if theSubItem contains "C" then
error number 1 -- exit all loops
end if
end repeat
on error number 2
log "Exited interior loop to exterior loop"
end try
end repeat
on error number 1
log "Exited all loops"
end try

"Matched " & subMatches & " subitems of " & tests & " tested items"


If you want to be extra clever, you can assign the error numbers to variables/properties to give them names.

-- 
Chris Page

 The other, other AppleScript Chris

 _______________________________________________
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

References: 
 >Re: How to do a goto with Applescript? (From: Skeeve <email@hidden>)
 >Re: How to do a goto with Applescript? (From: "Mark J. Reed" <email@hidden>)

  • Prev by Date: Re: Variable names
  • Next by Date: Re: Variable names
  • Previous by thread: Re: How to do a goto with Applescript?
  • Next by thread: Re: How to do a goto with Applescript?
  • Index(es):
    • Date
    • Thread