I was convinced some black magic was happening since my variable that I set to the value .lan and literally the value .lan are giving different results.
I added the final lines naming the variable something else and, bam, it works…
I’ve checked this with other things as well like
:put [find comment="$comment"]
and it does just return every single ID, even if the variable is empty/undefined. It looks to me like find itself is setting these values internally and then running into some odd behaviour like this.
Honestly I’m not sure what is happening internally and why it works like this, but I really think it shouldn’t. The substitution should happen before or as soon as we enter the internal scope of “find”. I would expect variable substitution to always be identical to substituting the same value by hand, if I give something an argument I should not have to care about what variable names are used internally by that command/function.
Without telling what you try to do, its hard to help.
To get anything correct out of RouterOS, you need to work with the id of an object.
Here is an example of getting some DHCP network information. (cut/past to terminal)
Use Splunk> to log/monitor your MikroTik Router(s).–> MikroTik->Splunk
Backup config to Gmail → Backup Block users that tries ut use non open ports → Block
It doesn’t matter I don’t want to argue about other languages, if the example is wrong or not,
but everyone should have common sense, not to call the variables with the usual name of the fields, then there is confusion.
On RouterOS it is like this, and here we are talking about RouterOS, not other languages.
Logical? For what? IS not clear what is it (and the right syntax if is wanted read/set the value is $this->“comment” or $this->$comment)
Then don’t talk about it as something that’s a thing in “all languages”. The common sense would be to expect it to behave as all other languages and for variables not to get magically overwritten. It is very common to name variables the same as the parameter or field name.
Is WANTED problem, not for create problems, but for use extended syntax on print and find, like my example,
on this way you can do operations on find, for not elaborate twice or more the results.
Some examples:
(on find)
# obtain identity from interface
{
:local int "ether5"
/ip neighbor
:foreach item in=[find where ($interface->0)=$int] do={
:put [get $item identity]
}
}
Why: “interface” is one array, on this way only the 1st value of the array is parsed
(1st find)
/ip dhcp-server lease
:foreach id in=[find where [:typeof $"active-address"]="ip"] do={
:local activeMAC [get $id active-mac-address]
:local if "undefined"
:local outstring "undefined"
/interface bridge host
:local searchresult [find where mac-address=$activeMAC]
:if ([:len $searchresult] > 0) do={
:set if [get $searchresult on-interface]
:set outstring "For bridge host table the $activeMAC is coming from $if"
} else={
:set outstring "Not find any result on bridge host table with MAC $activeMAC"
/ip arp
:set searchresult [find where mac-address=$activeMAC]
:if ([:len $searchresult] > 0) do={
:set if [get ($searchresult->0) interface]
:set outstring "$outstring, but for ARP table $activeMAC is coming from $if"
} else={
:set outstring "$outstring, neither on ARP table."
}
}
:put $outstring
}
Why: Return values only if “active-address” is one IP
Another example (on find)
/ip firewall address-list
add address=79.116.0.0/16 list=checklist
add address=86.127.0.0/16 list=checklist
add address=192.168.0.0/16 list=checklist
:local logIp 86.127.55.66
:if ([:len [find where list=checklist and (($logIp in $address) or ($logIp = $address))]] > 0) do={
:put "$logIp found inside one, or more, address pool in checklist"
}
The way I think about this is that commands are functions, and the standard arguments are “named parameters” to it. And inside a (user-defined) function, the argument names are local variables, so kinda make sense that built-in commands (i.e. function-like) would follow similar scoping rules as user-defined functions.