I’m currently slightly confused about the find command.
As I understand, the print command in general has a filtering syntax by using the where option, e.g.:
/ip/route/print where dst-address ~"^10.41"
When it now comes to other commands that takes ID’s I would use the find command as a subcommand. But the autocompletion suggests me also the “where” option. When typing this it yields the same behavior. In the documentation there’s nothing about find where. Why is this suggested by autocoplete? Does this yield different behavior?
:put [/ip/route/find dst-address ~"^10.41"]
:put [/ip/route/find where dst-address ~"^10.41"]
Addendum: Is there also an option to shortcut this regular expression? To allow partial/first match when using find?
There are many other places in RouterOS scripting where some keywords are optional. Maybe where used to be mandatory after find and now it remains only for backward compatibility reasons, like hide-sensitive which does nothing (as hiding sensitive information during export finally became the default and old recipes from the forum stopped working all of a sudden as hide-sensitive was an unknown parameter).
As for the “partial/first match”: the result of find, even if a single value is found, is a list, and automatic type conversion is often used:
[me@myTik] > :put [ip/address/find where address~"192.168.94.22"]
*2
[me@myTik] > :put ([ip/address/find where address~"192.168.94.22"]->0)
*2
[me@myTik] > :put [ip/address/find where address~"192.168"]
*2;*4;*5;*6
[me@myTik] > :put ([ip/address/find where address~"192.168"]->0)
*2
To make it even more entertaining, the find keyword is overloaded, as it also allows to look for a substring in a string:
HI @sindy,
the result of your test is the same using find or find where?
I’m trying to understand the difference between the find and find where commands, recently came across an old discussion (can’t find the link right now) that suggested find is used for single results, like:
find name=ether1
while find where is used for filtering, like:
find where name~"ether"
I’ve found where useful not with find or print but also in this case:
/system script export file=TEST where name=TestScript
Can you confirm if it’s still necessary to use find where or print where?
I have never seen any difference betwen the results of find and find where.
So I’ve tried right now, and that suggestion doesn’t seem to be useful: [me@myTik] > :put [ip/address/find where address~“192.168”]
*2;*4;*5;*6;*7;*10;*13
[me@myTik] > :put [ip/address/find address~“192.168”]
*2;*4;*5;*6;*7;*10;*13
So the find returns a list (with identical contents) in both cases. In your example, the actual difference is not the presence or absence of the where keyword but the operator - = yields true on exact match, ~ yields true for anything that matches the regular expression at the right side of the operator.
Thank you! I have not spotted yet that this possibility has been added in RouterOS 7. Yet another tiny improvement that makes life easier.
I cannot “confirm” anything as I am not a Mikrotik developer, but I can see a pattern now: if find is used to create a list of leaf objects of the configuration tree, all that can follow the keyword find is the condition expression, whereas both the print and export keywords may be followed by other parameters (like detail, show-sensitive etc.) as well, so the where is used as an unambiguous delimiter, i.e. an indication that whatever follows it are match conditions.