won’t spoil it, but just rename the function…
Okay, I’ll dig around myself, maybe it will work…
I already gave you the “solution” in the previous post…
I didn’t miss this… By :parse inside :parse you mean something like this construction:
[[:parse "[:parse [/system script get $msgTxt source]] $Parametr1 $Parametr2"]]
I thought about it, but it turned out, as always, only after Rex’s blessing:
:global AnyName do={
:if ($1="test") do={
[[:parse [:parse ":global [:pick \$$0 1 [:len \$$0]]"]]];
[[:parse [:parse "[:pick \$$0 1 [:len \$$0]]"]]]
:put "Recursive call without mentioning the function name works"}
:put OK
}
It turns out that I was happy early. The syntax of the constructs passes, but the execution does not. We still need to dig deeper.
It turned out to be much simpler. To call a function recursively without specifying its name, you don’t need to declare it internally at all. Simple enough:
[$0]
:global AnyName do={
:if ($1="test") do={
[$0]
:put "Recursive call without mentioning the function name works"}
:put OK
}
Now it works, but only without passing an argument.
you can pass arguments during a recursive call like this:
[[:parse "global $[:pick $0 1 [:len $0]]; [$0 mama]"]]
This example illustrates the correct solution:
:global CountDown do={
:if ($1>0) do={:put $1; :set $1 ($1-1);[[:parse "global $[:pick $0 1 [:len $0]]; [$0 $1]"]];}
:return "end"
}
:put [$CountDown 5]
5
4
3
2
1
end
If you need to pass more arguments or/and named arguments, all of them must be specified in :parse
In general, the final phrase for declaring and immediately calling a recursive function call without specifying its name is as follows:
[[:parse "global $[:pick $0 1 [:len $0]]; [$0 $1 $2 $Par1 $Par2]"]]
There is a problem interpreting a string that contains arbitrary variables: what if $Par1 is “)” ?
I like your AnyName example much more, but it works only with one recursive call, because when calling recursively $0 no longer is $AnyName and cannot call itself once again. But it is easy to correct, here is how I would define the countdown function based on this idea:
:global countdown do={
:put " \$0: $0 ($[:typeof $0])"
:put " \$1: $1 ($[:typeof $1])"
:put $1
:if ($1>0) do={
[$0] ($0) ($1-1)
} else={
:put "end"
}
}
[admin@labISP] > $countdown 5
$0: $countdown (lookup)
$1: 5 (str)
5
$0: $countdown (lookup)
$1: 4 (num)
4
$0: $countdown (lookup)
$1: 3 (num)
3
$0: $countdown (lookup)
$1: 2 (num)
2
$0: $countdown (lookup)
$1: 1 (num)
1
$0: $countdown (lookup)
$1: 0 (num)
0
end
[admin@labISP] >
It is also interesting that when executing [$0] A B the first parameter $1=B and when executing $countdown 5, $1=5.
Or just create recursion like here…
:execute “:global $varName "$varValue"”;