nil is not the same as nothing:
Wiki docs currently state:
Apparently that's not true exactly, at least with RouterOS 6.22:nil - default variable type if no value is assigned;
Code: Select all
{:local noValAssigned; :put "noValAssigned='$noValAssigned' ($[:typeof $noValAssigned],$[:len $noValAssigned])"}
Code: Select all
noValAssigned='' (nothing,0)
Code: Select all
{
:local a [:pick "" -1];
:put ":pick example: a='$a' ($[:typeof $a],$[:len $a])";
:set a [:find [/ip dns get servers] "10.0.0.1" -1];
:put ":find example: a='$a' ($[:typeof $a],$[:len $a])";
}
Code: Select all
pick example: a='' (nil,0)
:find example: a='' (nil,0)
Code: Select all
{
:local weKnowThisIsNil [:pick "" -1];
:local isItNil [:find {"foo";"bar"} "baz" -1];
:if ([:typeof $isItNil]="nil") do={
:put ":typeof test: It IS nil!";
};
:if ([:len $isItNil]=0) do={
:put ":len test: It IS nil! (WARNING: Method NOT recommended!)";
};
:if ($isItNil=$weKnowThisIsNil) do={
:put "compare against known nil value test: It IS nil! (I prefer this method.)";
};
}
Code: Select all
:typeof test: It IS nil!
:len test: It IS nil! (WARNING: Method NOT recommended!)
compare against known nil value test: It IS nil! (I prefer this method.)
The same techniques work to test for undefined variables (the special nothing value):
Code: Select all
{
:local knownNothing;
:local isItNothing;
:if ([:typeof $isItNothing]="nothing") do={
:put ":typeof test: It IS nothing!";
};
:if ([:len $isItNothing]=0) do={
:put ":len test: It IS nothing! (WARNING: Method NOT recommended!)";
};
:if ($isItNothing=$knownNothing) do={
:put "compare against known nothing value test: It IS nothing! (I prefer this method.)";
};
}
Code: Select all
:typeof test: It IS nothing!
:len test: It IS nothing! (WARNING: Method NOT recommended!)
compare against known nothing value test: It IS nothing! (I prefer this method.)
For scripting, could you make global constants named NOTHING and NIL available for doing comparisons with? So code like this would work:
Code: Select all
:if ($var=NOTHING) do={ ... } else={:if ($var=NIL) do={ ... } }