Can somebody explain how this script works?

How define dynamic variables:
http://forum.mikrotik.com/t/concatenate-values-to-create-variable-name/151793/2


You do not specify where you try to run this “script”, I assume the Terminal at this point.

Script analysis. part one:

:local cookie "someName";
:local cookieCode do={
 :put "TestIt";
 /system routerboard print;
};

[[:parse (":global ".$cookie)]];
[[:parse (":global ".$cookie."; :set \$".$cookie." (\"\");")]];

/system script environment remove [find where name=$cookie];
  1. remove all frills
  2. replace long chain of statements like “1 + 1 + 1 + 1 + 1” with a chain that does the same thing, but shorter like “1 × 5” (just an example)
  3. fix $ error after :set and added missing { } for terminal.


{
:local cookie "someName"

:local cookieCode do={:put "TestIt"; /system routerboard print}
# create a function called cookieCode and the function do what is present between the { }

[[:parse ":global $cookie"]]
# is like >:global someName<
# what this do: on parse session define global variable someName with "nothing" value
 
[[:parse ":global $cookie; :set $cookie \"\""]]
# is like >:global someName; set someName ""<
# what this do: on parse session define global variable someName with "nothing" value,
# and later set someName global variable = to "" (empty string)

/system script environment remove [find where name=$cookie]
# what this do: remove global variable "someName" from environment
}

But we don’t give a damn about all this, in reality what we notice is that the function is called even if it is never written to use it:

For extremely synthesize the compiler bug:
[9b184@MATRIX] > :local test do={:put “WHY\3F\3F\3F”};;
WHY???
[9b184@MATRIX] >

Part two is just the same as part one, just multiplied:
{
:local test1 do={:put “test1”}
:local test2 do={:put “test2”}
:local test3 do={:put “test3”}
:local test4 do={:put “test4”}
:local test5 do={:put “test5”}


}
on second call all function defined before are executed…


Temporary (I hope) fix: assign the returned value to one useless variable:

{
:local cookie "someName"

:local cookieCode do={:put "TestIt"; /system routerboard print}

:local useless [[:parse ":global $cookie"]]
 
:local useless [[:parse ":global $cookie; :set $cookie \"\""]]

/system script environment remove [find where name=$cookie]
}

Tested on 6.48.6 and 7.6