Now that MikroTik scripting allows passing around code chunks and makes it more easy to create them by setting variables (no more complex parsing of strings with crazy escape sequences), I’d like to see MikroTik add anonymous inline code abilities that don’t need a variable. For example:
{
:local Map do={
:local list $1;
:local out [:toarray ""];
:if ([:typeof $list]!="array") do={
:set list ([:toarray ""],$list); ## In case input is a single item (non-array)
};
:do {
:foreach k,v in=$list do={
:set out ($out,[$2 $k $v]);
};
} on-error={
:log error "Map(array,func) failed to execute func for array";
};
:return $out;
};
:local func do={
:return $2;
};
:local citystateassoc {"Houston"="Texas";"Denver"="Colorado";"Washington"="D.C.";"Las Vegas"="Nevada";"Portland"="Maine";"Dallas"="Texas"};
:local states [$Map $citystateassoc $func];
:put "STATES (in no particular order, may be duplicates):"
:put $states;
}
WITH inline anonymous code blocks able to be passed as function arguments, I could instead do:
:local citystateassoc {"Houston"="Texas";"Denver"="Colorado";"Washington"="D.C.";"Las Vegas"="Nevada";"Portland"="Maine";"Dallas"="Texas"};
:local states [$Map $citystateassoc [:func do={:return $2;}]];
:put "STATES (in no particular order, may be duplicates):"
:put $states;
The relevant bit is the [:func do={:return $2;}] (or it could be called :lamda, :proc, :code, etc…). The command would return a special code value (or like the do={} variable setting stuff, return an array of length 2 containing {nil;(code)}).
How about it?