mIRC Homepage
Posted By: Bilge Recursion - 02/07/04 06:20 PM
I have just built a fairly complex recursion routine but it seems that I get an error when the alias calls itself - mIRC sends the name of the alias to the server instead of recalling it. Is this possibly due to an error in my script, which I can't seem to find, or is this because mIRC does not support recursion?
Posted By: starbucks_mafia Re: Recursion - 02/07/04 06:50 PM
From the help file:
An alias cannot call itself recursively mainly because this seems to cause more problems for users than it solves.

You can get around this by using scon/scid with the command, or you can use simple indirect recursion (ie. /alias1 calls /alias2 which calls /alias1 again). Be aware though that with both of these methods you're calling two commands for each level of recursion which means you can only have a maximum recursion depth of 500.
Posted By: Bilge Re: Recursion - 02/07/04 06:58 PM
The /scid workaround would be nice if it worked, but it does not. This does allow the command to call itself but it does not allow the first instance of the alias to continue when all child instances have completed.

If it might help then I will include the code:
alias lema.recurse.thememsg.buildbracematrix {
  ;Usage: %ht %node %msg
  var %ht = $1, %level = $2, %j = 1, %node, %re = /(\([^(]*?((?:\(.*\))?)[^(]*?\))/g
  tokenize 32 $3-

  ;In the following regex all odd numbers contain level root_level braces, all even numbers contain root_level+1 braces or null if no nested braces
  ;Because root_level+1 may also have a +2 level and +3 and so on, recursion must be used until all even numbers return $null
  void $regex($1-,%re)
  while (%j <= $regml(0)) {
    %node = $+($iif(%level,$+(%level,.)),$round($calc(%j / 2),0))
    if ($isodd(%j)) hadd %ht %node $regml(%j)
    elseif ($regml(%j)) scid $cid lema.recurse.thememsg.buildbracematrix %ht %node $regml(%j)
    inc %j
The alias is first called from another alias to initialise it and is initialised as follows:
lema.recurse.thememsg.buildbracematrix %ht 0 $1-
where %ht is a hash table name and $1- is some data containing nested parenthesis.
Posted By: qwerty Re: Recursion - 02/07/04 08:07 PM
Without carefully examining your code, I noticed a potential problem with $regml(). $regml() is not local to the routine that calls it, it's like $ifmatch and global variables. Meaning that a child /lema.recurse.thememsg.buildbracematrix would corrupt the while (%j <= $regml(0)) of the parent. You need to use unique names with $regex/$regml to get around this problem ( $regex(name,text,/pattern/) ). From what I see, %node would be a good candidate for a named $regex.
Posted By: Bilge Re: Recursion - 02/07/04 08:25 PM
Thank you both for your great help! I think you meant %level rather than %node... it's a bit quirky. %node gets passed back to the function as %level, and then when it is modified becomes %node. I originally called it %level and %newlevel. I guess that way it made more sense.

Anyway it works properly now thanks to the combination of both of your tips. Cheers guys.
Posted By: Bilge Re: Recursion - 05/07/04 01:50 AM
What about if I need to call a custom identifier which provides a return value recursively? $scid($cid) properties cannot contain brackets and therefore no parameters can be passed to the identifier, and they need to be.
Posted By: starbucks_mafia Re: Recursion - 05/07/04 04:24 PM
I think in that case your only option is to use the indirect recursion method. alias1 returns alias2 returns alias1
© mIRC Discussion Forums