[comp.sys.mac.hypercard] recursion

V050FN5R@UBVMS.BITNET (Maurice Volaski) (04/17/89)

I am trying to write a function that does search and replace of strings in
a stack.
     
Basically, it is a function of the form
replace "wordToFind","howToLook","replaceWithThis"
     
Once entered into the message box, the the function begins to work, but
after a number of times, it stops with a too much recursion error. Does
anyone have an idea why?
     
Internally, the function puts the wordToFind and ReplaceWithThis into
global variables then proceeds to find wordToFind. It then gets the position
of this wordToFind in a field that the find command found it in by using
the foundchunk. It replaces the wordToFind that is in the field at that
location, then proceeds by calling finding again recursively.
     
It may be that the calling of find is being nested many times. Is there
any way to make act as if it were being called fresh each time?
     
     
Thanks a lot for any help.
Maurice

martin@m2.csc.ti.com (Steven Martin) (04/17/89)

In article <9984@netnews.upenn.edu> V050FN5R@UBVMS.BITNET (Maurice Volaski) writes:
>I am trying to write a function that does search and replace of strings in
>a stack.
>     
>Basically, it is a function of the form
>replace "wordToFind","howToLook","replaceWithThis"
>     
>Internally, the function puts the wordToFind and ReplaceWithThis into
>global variables then proceeds to find wordToFind. It then gets the position
>of this wordToFind in a field that the find command found it in by using
>the foundchunk. It replaces the wordToFind that is in the field at that
>location, then proceeds by calling finding again recursively.
>     
>It may be that the calling of find is being nested many times. Is there
>any way to make act as if it were being called fresh each time?

The problem is not with nesting of FINDs, it is that you are calling
replace recursively.  Hypercard can only handle a small number of
recursions so you need to rewrite your handler to use a repeat loop
like this.

on replace wordToFind,replaceWithThis
  -- find the first one
  find word wordToFind
  -- stop when the result is "not found"
  repeat while the result is empty
    -- execute "put replaceWithThis into char x to y of field z"
    do "put replaceWithThis into" && the foundchunk
    -- find the next one
    find word wordToFind
  end repeat
end replace
    
That should do the trick!

Steve Martin            USENET: {ctvax,im4u,texsun,rice}!ti-csl!martin
                        ARPANET: SMARTIN@CSC.TI.COM  COMPUSERVE: 72727,1471
                        GENIE: S.MARTIN8    PHONE: (214)-995-0698, 404-1061
What I am is what I am, are you what you are or what?