Jump to content

Any way to get recursion level easy


Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

#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

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...