Persistent Variables during recursion

Bob Sneidar bobs at twft.com
Thu Jan 26 17:06:00 EST 2012


Thanks John I just had figured out that the way to do it was to pass the values as arguments in the recursion. 

Bob


On Jan 26, 2012, at 12:34 PM, John Craig wrote:

> Hi, Bob.  Here's one way of doing it.  In the first call, pIndex is empty, so some initialization is performed.  The parameters are all passed with each recursive call, but pSame is always the same value.
> 
> HTH
> 
> 
> :)
> 
> 
> 
> on mouseUp
>   test "12345", 1, 99
> end mouseUp
> 
> 
> command test pData, pChange, pSame, pIndex
>   if pIndex is not an integer then
>      put 1 into tpIndex
>      put "first time" & LF into msg
>   end if
> 
>   put char pIndex of pData after pData
>   put pIndex ^ 2 into pChange
>   add 1 to pIndex
> 
>   put pData & ", " & pChange & ", " & pSame & ", " & pIndex & LF after msg
> 
>   if length(pData) < 10 then
>      test pData, pChange, pSame, pIndex
>   end if
> end test
> 
> 
> On 26/01/2012 20:04, Bob Sneidar wrote:
>> Hi all. This is my first foray into recursive functions. There are two variables I would like to be persistent for as long as the script is running, but I want them to be empty the first time I call the recursive function. I have tried declaring the variables as local, but each time the function calls itself, the variables are wiped as though a new set is made. If I declare them as global, then of course the value persists even after the last recursion terminates. The function has no idea if it is  the first time through or one of the iterations. Any idea how to get around this?
>> 
>> Bob
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list