corgano

Any way to get recursion level easy

9 posts in this topic

#1 ·  Posted

Autoit has a feature where if you create a recursive function with no exit, it will limit how many times it recurses to  3898, then it exits the program.

My question / feature request is A: How does autoit track this? Is there some kind of internal counter that +1 every time it enters a func and -1 every time it returns from one?
And B: IF this counter exist, is there a way to access it? IF not, could it be made accessible with a @macro?


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites



#2 ·  Posted

Things don't work that way. A fixed-size call stack is used and is consumed by function calls, each eating a variable (I guess) amount of stack space depending on parameters. There is nothing under your control here. If you expect to exhaust stack space by recursion, then de-recurse your algorithm(s).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted

It's not that I'm having the problem in particular, I was just wondering how autoit keeps track. So in that case, even a string of non-recursive functions long enough could trigger the same limit? Cool.


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

#4 ·  Posted

That would be a huge function call depth! I can't imagine such a situation being any close to the real world.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#5 ·  Posted

#cs
    A function is recursive when the same Func runs many times, self called from within itself.
    Variables declared as Global at the top of the script are able to be accessed from any instance of the function,
    whereas variables declared (as Local) within the function may be different for each instance of the function.
    so simply declare a Global var to keep track of the recursion level. (Increment that variable at the start of the function)
#ce

Global $iRecursionLevel = 0 ; global variable accessible from any instance

ConsoleWrite("Entering recursion" & @CRLF & "------------------" & @CRLF)
_Recursive()
ConsoleWrite("------------------" & @CRLF & "Recursion finished. Bye" & @CRLF)

Func _Recursive($iPreviousLevel = 0)

    $iRecursionLevel += 1 ; keep track of the recursion level (global variable)

    ; following variables are recreated for each instance
    Local $sTime = @HOUR & ":" & @MIN & ":" & @SEC
    Local $iInstance = $iRecursionLevel

    ConsoleWrite("Message from instance " & $iInstance & " entered at " & $sTime & @CRLF)

    Sleep(1500)

    ; recurse for max 8 levels deep
    If $iPreviousLevel < 7 Then _Recursive($iInstance) ; --- recursion --->

    ; we are here when exiting from previous recursions
    Sleep(500)
    ConsoleWrite("This is instance " & $iInstance & ". I was called at " & $sTime & @CRLF)
EndFunc   ;==>_Recursive

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I would simply go for this taking @Chimp's example:

#cs
    A function is recursive when the same Func runs many times, self called from within itself.
    Variables declared as Global at the top of the script are able to be accessed from any instance of the function,
    whereas variables declared (as Local) within the function may be different for each instance of the function.
    so simply declare a Global var to keep track of the recursion level. (Increment that variable at the start of the function)
#ce

Global $iRecursionLevel = 0 ; global variable accessible from any instance

ConsoleWrite("Entering recursion" & @CRLF & "------------------" & @CRLF)
_Recursive()
ConsoleWrite("------------------" & @CRLF & "Recursion finished. Bye" & @CRLF)

Func _Recursive()
    $iRecursionLevel += 1 ; keep track of the recursion level (global variable)
    ; following variables are recreated for each instance
    Local $sTime = @HOUR & ":" & @MIN & ":" & @SEC
    ConsoleWrite("Message from instance " & $iRecursionLevel & " entered at " & $sTime & @CRLF)
    Sleep(1500)
    ; recurse for max 8 levels deep
    If $iRecursionLevel < 7 Then  _Recursive() ; --- recursion --->

    ; we are here when exiting from previous recursions
    Sleep(500)
    ConsoleWrite("This is instance " & $iRecursionLevel & ". I was called at " & $sTime & @CRLF)
    $iRecursionLevel -= 1 ; keep track of the recursion level (global variable)
EndFunc   ;==>_Recursive

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#7 ·  Posted

You also can use a Local Static variable.

 

Saludos

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This is how you do it.
An example of where a static local is really useful

_MyRecusiveFunction(20,True)

_MyRecusiveFunction(10,True)

Func _MyRecusiveFunction($MaxRecursion = 100,$bReset = false)
Static Local $iRecursionCount

    If $bReset Then $iRecursionCount = 0
    If $iRecursionCount >= $MaxRecursion Then Return

    $iRecursionCount += 1
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $iRecursionCount = ' & $iRecursionCount & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

    _MyRecusiveFunction($MaxRecursion)

EndFunc

Edit: Change to allow the count to be reinitialised
Edit: Changed name of function to reflect the purpose of the example as jchd pointed out, below, the original name (_MyIterativeFunction) I used was a poor and misleading choice on my part..

Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#9 ·  Posted

Nitpick: the above function name is a poor choice since it isn't iterative but recursive.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now