• 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: Breaking out of recursive handler and returning result does not happen
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Breaking out of recursive handler and returning result does not happen


  • Subject: Re: Breaking out of recursive handler and returning result does not happen
  • From: Zack Jarrett <email@hidden>
  • Date: Sun, 23 Oct 2011 15:38:14 -0700

When the runtime begins extricating itself from the recursive stack you need to make sure that each recursion in the stack hands it's return object up to its parent.

In your case your code is dropping the ball at 

my _parseXMLElements(currentItem2)

inside of _parseXMLElements().  You're not keeping track of the object returned by your recursive call.  Whatever gets returned by the called instance of _parseXMLElements() is disappearing as soon as the runtime returns control to the calling instance.

You'll have to do something like:
return my _parseXMLElements(currentItem2)

That way whatever is returned by _parseXMLElements() will be passed back up the stack.  You should make sure that something is always passed up the recursive stack.

Check out the below example I just whipped up.  Recursion is always tricky.  Good luck!

Zack

P.S.  I feel like every time I build something recursive I have to invent the wheel all over again.  There are lots of ways to skin a recursive cat.
P.P.S.  It's often best to avoid recursion all together.  If you can avoid it you should, even if the end result isn't as elegant as it could be.


--This is a ridiculous example of using recursion to find
--the first word that starts with f in a string.
--No one in their right mind would solve this problem this way.
--It is written as an example only.

set theString to "Zaphod is a really amazingly cool frood."

--this is a semaphore for the recursion.  If keepGoing is true then keep going.
property keepGoing : true

set theResult to findFirstWordStartingWithF(words of theString)

--Check to make sure theResult is valid
if theResult = -1 then
error "ack, no words starting with f were found"
else
--do stuff with theResult
return theResult
end if

on findFirstWordStartingWithF(wordList)
--Make sure the wordList is not empty
if length of wordList > 0 then
set currentWord to item 1 of wordList


if currentWord starts with "f" then
set keepGoing to false
return currentWord
end if


repeat while keepGoing
set newWordList to rest of wordList
--We make sure to return the result of the recursive call
--This way the result gets passed up the stack
return findFirstWordStartingWithF(newWordList)
end repeat
else
--The list is empty.  It has no words.
--Return a value that is not valid in the context
return -1
end if
end findFirstWordStartingWithF
 _______________________________________________
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: 
 >Breaking out of recursive handler and returning result does not happen (From: Andreas Mixich <email@hidden>)

  • Prev by Date: Breaking out of recursive handler and returning result does not happen
  • Next by Date: Re: [ANN] Free app lets you run ASObjC code from menus/panels
  • Previous by thread: Breaking out of recursive handler and returning result does not happen
  • Next by thread: Remote Disks
  • Index(es):
    • Date
    • Thread