Community discussions

MikroTik App
 
User avatar
payday
Member Candidate
Member Candidate
Topic Author
Posts: 233
Joined: Thu Aug 16, 2012 11:05 pm

How to check if value is empty?

Sat Nov 09, 2013 12:55 am

In RouterOS version before 6.2 the following command returned items with empty comments:
/interface> print count-only where !comment
3
In current version of ROS it always returns 0. Don't know if it is a bug or new feature. :)
How to produce the same old result using current version?
What condition should be put in where to check if comment is empty?
Last edited by payday on Sun Nov 10, 2013 10:36 pm, edited 1 time in total.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: How to check if value is empty?

Sat Nov 09, 2013 11:56 am

"!comment" is supposed to find items where a comment is not set. Previously, "not set" was treated as an equivalent of "empty", but that's not technically true, so this new behavior is IMHO the correct one.

To check for empty-ness, just compare against an empty string, i.e.
print where comment=""
 
User avatar
payday
Member Candidate
Member Candidate
Topic Author
Posts: 233
Joined: Thu Aug 16, 2012 11:05 pm

Re: How to check if value is empty?

Sun Nov 10, 2013 11:06 pm

I've checked that and it does not work.
I've also checked this:
/interface wireless access-list> print count-only where !(comment)
0
/interface wireless access-list> print count-only where (comment) 
0
/interface wireless access-list> print count-only where comment  
0
/interface wireless access-list> print count-only where comment=""
0
/interface wireless access-list> print count-only where comment=nil
0
/interface wireless access-list> print count-only where comment=nothing
0
/interface wireless access-list> print count-only where comment=:nothing
0
/interface wireless access-list> print count-only where comment=:nil    
0
/interface wireless access-list> print count-only where !comment   
0
/interface wireless access-list> print count-only where (!comment)
0
I have two items without comment so I am expecting to get 2 as a result.
 
User avatar
payday
Member Candidate
Member Candidate
Topic Author
Posts: 233
Joined: Thu Aug 16, 2012 11:05 pm

Re: How to check if value is empty?

Sun Nov 10, 2013 11:31 pm

BINGO! :)
Trying other combinations:
/interface wireless access-list> print count-only where not comment
0
/interface wireless access-list> print count-only where comment~"" 
89
Last one returned numer of all items on the list - with and without comment - strange.
So, another try:
/interface wireless access-list> print count-only where !comment~""
2
This returned numer of items without comment. :)

This is working on ROS 6.6. I will see for how long will it work (until MikroTik change it again)....
Last edited by payday on Thu Nov 14, 2013 10:48 pm, edited 1 time in total.
 
User avatar
vipe
Member Candidate
Member Candidate
Posts: 166
Joined: Thu Sep 14, 2006 10:05 pm

Re: How to check if value is empty?

Mon Nov 11, 2013 11:54 am

These no longer work in v6.2 and v6.3:
:put [:len [/ip route find !routing-mark]]
:put [:len [/interface find !comment]]

The alternative syntax suggested in viewtopic.php?f=2&t=75894, with parentheses around !routing-mark and !comment, always returns nothing (len=zero).

Is this a bug or am I missing something?

Thanks for your help...




5.26

Similarly for !status
:put [:len [/ip dhcp-server lease find !status]]

( :put [:len [/ip dhcp-server lease find status=""]]
does not return any results either )

This seems to be a generic issue with the not operator when used in a [set/get find] and [print where] context. Am I right?
 
User avatar
paoloaga
Member Candidate
Member Candidate
Posts: 227
Joined: Tue Mar 08, 2011 2:52 am
Location: Lugano - Switzerland
Contact:

Re: How to check if value is empty?

Fri Apr 18, 2014 9:55 pm

/interface wireless access-list> print count-only where !comment~""
2[/code] This returned numer of items without comment. :)

This is working on ROS 6.6. I will see for how long will it work (until MikroTik change it again)....
I think that's better this way:

:put [/ip route find where dst-address="0.0.0.0/0" && routing-mark~"^\$" ]

because the "~" operator matches against a regular expression. To match an empty string you can use "^\$". I think that using !item~"" works by luck.
 
Chupakabra303
just joined
Posts: 14
Joined: Tue Jun 20, 2017 3:07 pm

Re: How to check if value is empty?

Thu May 16, 2019 4:47 pm

Magic word "any":
[find where !any routing-mark and dst-address=0.0.0.0/0 and active=yes]
 
Jooocker
just joined
Posts: 6
Joined: Fri Nov 15, 2019 1:09 pm

Re: How to check if value is empty?

Sun Aug 30, 2020 10:47 pm

You can use [] to refer to empty value.
LIKE THIS:
where !comment=[]

Example:
foreach i in=[ /tool user-manager user find where comment=[] ] do={ put [ /tool user-manager user get $i username ] }

This will give you all usernames they don't have comment .
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1071
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: How to check if value is empty?

Wed Sep 02, 2020 12:26 am

For the inversion you have to use parenthesis:
... where !(comment=[])
Really nice to have this... Is this documented anywhere?
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1071
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: How to check if value is empty?

Wed Sep 02, 2020 12:30 am

Answering myself... Looks like this is executing an empty command, which evaluates to "nil":
[admin@mt] > :put [ :typeof [] ]
nil

Who is online

Users browsing this forum: No registered users and 40 guests