Community discussions

MikroTik App
 
dhiaahmed
newbie
Topic Author
Posts: 44
Joined: Sat Mar 31, 2018 1:05 pm

Checking the empty value

Fri Nov 20, 2020 11:39 am

I don't know a lot about scripting in mikrotik and I really need to know how to check the empty value.
For example: when I write it like this it doesn't work I don't know why!!
global z ; 
if ($z="") do={put "hello world"}
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Checking the empty value

Sun Nov 22, 2020 5:58 pm

Because your variable is nothing, but you check for an empty string. That's something different. Try this:
:global z; 
:if ($z=[]) do={:put "hello world";}
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Checking the empty value

Sun Nov 22, 2020 7:06 pm

Some other ways to check:

([:len $z]=0) works also for array
([:typeof $z] ~ "(nil|nothing)")
I did not knew the =[ ] and that one could replace the :len one for me.
 
dhiaahmed
newbie
Topic Author
Posts: 44
Joined: Sat Mar 31, 2018 1:05 pm

Re: Checking the empty value

Fri Nov 27, 2020 11:23 am

I've tried this but it didn't work :(
:global z; 
:if ($z=[]) do={:put "hello world";} 
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Checking the empty value  [SOLVED]

Sun Nov 29, 2020 1:24 am

Damn, I've typed this from a mobile without checking... You are right, it does not work. Sorry for the confusion.

To go into detail...
[admin@MikroTik] > :global z; 
[admin@MikroTik] > :put [ :typeof $z ]
nothing
[admin@MikroTik] > :put [ :typeof [] ]  
nil
There's a difference between "nothing" and "nil"... To check for nothing you have to use :typeof I guess...
:global z; 
:if ([ :typeof $z ] = "nothing") do={:put "hello world";}
 
dhiaahmed
newbie
Topic Author
Posts: 44
Joined: Sat Mar 31, 2018 1:05 pm

Re: Checking the empty value

Wed Dec 02, 2020 12:40 pm

Thank u so much. It's working now
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Checking the empty value

Wed Dec 02, 2020 1:22 pm

;-)

Who is online

Users browsing this forum: diamuxin and 23 guests