Community discussions

MikroTik App
 
changeip
Forum Guru
Forum Guru
Topic Author
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

undefined variables

Thu Aug 16, 2007 12:14 am

In 3.0rc2 how can I determine if a global variable exists or not:

[x@x] > :put [:typeof $anything]
syntax error (line 1 column 16)

In 2.9.x it would return 'nil'... How can I determine if a variable exists or not? I believe this is happening because 3.0 is parsing the variable name for screen validation.

:if ([ :typeof $anything ] = nil ) do={ :global anything 0.0.0.0/0 }

Thx,
Sam
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7041
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: undefined variables

Thu Aug 16, 2007 10:30 pm

Variable will exist anyway, because in 3.0 all variables must be defined before used in script.
 
changeip
Forum Guru
Forum Guru
Topic Author
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Re: undefined variables

Thu Aug 16, 2007 11:36 pm

i know that i can define it, however i wish to check if it's already been initialized. I don't want to overwrite the value if it's already there.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7041
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: undefined variables

Fri Aug 17, 2007 1:11 pm

if you have defined variable like this:
:global strVar "my string";

next time you do:
:global strVar;
it's value will remain previous, in this case "my tring".

I hope you understand what i mean.

You can check only if variable has no value:

:if ( $strVar = "" ) do= { .... }
 
changeip
Forum Guru
Forum Guru
Topic Author
Posts: 3830
Joined: Fri May 28, 2004 5:22 pm

Re: undefined variables

Fri Aug 17, 2007 9:44 pm

ah, i will try that. if i can declare the variable with nothing and have it stay the same that will work. Thank you.
 
lasa
just joined
Posts: 6
Joined: Mon Dec 28, 2020 5:55 pm

Re: undefined variables

Tue Feb 02, 2021 11:45 am

I have same question
because
when the router shut down global variables will be clear
so i want to check "does global variables is exist?"
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: undefined variables

Tue Feb 02, 2021 7:19 pm

Its already posted above on how to test for a variable.
:if ( $strVar = "" ) do= { .... }

Who is online

Users browsing this forum: Bing [Bot] and 10 guests