Community discussions

MikroTik App
 
dhiaahmed
just joined
Topic Author
Posts: 12
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 Veteran
Forum Veteran
Posts: 724
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";}
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
msatter
Forum Guru
Forum Guru
Posts: 1982
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.
One RB4011 (cooled) and a RB760iGS (hEX S) in series. The 4011 Does PPPoE/IKEv2.
The cooler: viewtopic.php?f=3&t=138613&start=300#p799879
Running:
RouterOS 6.48beta58 / Winbox 3.27 64bits / MikroTik APP 1.3.15
 
dhiaahmed
just joined
Topic Author
Posts: 12
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 Veteran
Forum Veteran
Posts: 724
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";}
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
 
dhiaahmed
just joined
Topic Author
Posts: 12
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: 1982
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Checking the empty value

Wed Dec 02, 2020 1:22 pm

;-)
One RB4011 (cooled) and a RB760iGS (hEX S) in series. The 4011 Does PPPoE/IKEv2.
The cooler: viewtopic.php?f=3&t=138613&start=300#p799879
Running:
RouterOS 6.48beta58 / Winbox 3.27 64bits / MikroTik APP 1.3.15

Who is online

Users browsing this forum: fusa and 31 guests