Community discussions

MikroTik App
 
txfz
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 66
Joined: Tue Mar 10, 2020 9:02 am

Best way to check whether [find] yields results?

Thu Oct 03, 2024 3:20 pm

Hi,

Because I can never remember how this works, I find myself using the following expressions interchangibly in order to determine whether one or more matching items exist:
    :if ([:len [/interface find name=$interfaceName]] > 0) do={
    :if ([/interface find name=$interfaceName] = "") do={
    :if [/interface find name=$interfaceName] do={
I would like to use the last option, but I don't know whether it always works as expected, nor whether these are exactly equivalent.

Edit: Although I apparently can't invert the last option easily, because I "cannot logically negate nothing". Second option is just awkward. First option is a little long, but feels the most robust, as one would expect [find] to always return an array of results.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12554
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Best way to check whether [find] yields results?  [SOLVED]

Thu Oct 03, 2024 5:25 pm

The correct method is only the first.
:if ([:len [/interface find where name="the name"]] > 0) do={} else={}

# or if used with print
:if ([:len [/interface print as-value where name="the name"]] > 0) do={} else={}
Stop.

Too often in scripts you get strange results when using an array and comparing it to simple strings:
:put ({} = "")
true

:put ([:toarray ""] = "")
true

Test for 3rd method:
:put ([:toarray ""] = true)
false

:put ({} = true)
false

:put ([:toarray "1,2,3,4"] = true)
false

:put ({1,2,3,4} = true)
false

Who is online

Users browsing this forum: gargiuseppe and 12 guests