Community discussions

 
User avatar
iperezandres
newbie
Topic Author
Posts: 30
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Type "nothing"

Sun Dec 09, 2018 2:36 am

With this code:
:global X1 1
:global type1 [:typeof $X1]
The type of X1 is "num".

But with this other code:
:global i 1
:execute ":global X$i"
:global type2 [:typeof $X1]
The type of X1 is "nothing" instead of "num", with X1 being previously declared as a global variable from other script.

¿Why the nothing type, if X1 already existed?

if you need to know, what I am trying to do is something like this, to reuse several global variables already declared from another script:
:for i from=1 to=4 do={
   :execute ":global IP$i"
}
 
2frogs
Member
Member
Posts: 422
Joined: Fri Dec 03, 2010 1:38 am

Re: Type "nothing"

Sun Dec 09, 2018 5:52 am

All variables have to be declared that are used in the script, global or not, declared in other scripts or not.
 
User avatar
Jotne
Forum Veteran
Forum Veteran
Posts: 707
Joined: Sat Dec 24, 2016 11:17 am

Re: Type "nothing"

Sun Dec 09, 2018 10:26 am

As 2frogs write, you need to declare it like this.
:global testvarr
If you add something after, you will overwrite current data.

So if in one script you have some like this:
:global testvar "my data"
Then run this in an other script:
:global testvar
:put $testvar
will give
my data
.
Use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
sebastia
Long time Member
Long time Member
Posts: 684
Joined: Tue Oct 12, 2010 3:23 am

Re: Type "nothing"

Sun Dec 09, 2018 1:03 pm

The question is why is the typing changing. Declaring a variable shouldn't be doing that.

Tried to replicate, but can't:

2 sessions/terminals

1. run first
[Sebastian@firewall] > :global X1 1
[Sebastian@firewall] > :put [:typeof $X1] 
num

2 run second
[Sebastian@firewall] > {:local i 1; :execute ":global X$i"; :put [:typeof $X1]}       
num
Last edited by sebastia on Sun Dec 09, 2018 1:43 pm, edited 1 time in total.
 
User avatar
Jotne
Forum Veteran
Forum Veteran
Posts: 707
Joined: Sat Dec 24, 2016 11:17 am

Re: Type "nothing"

Sun Dec 09, 2018 1:21 pm

I do not understand your problem. Test this.

First script, storing data
{
:put "storing global variables"
:global text "my text"
:global number 2345
:put "Test of text: $text = $([:typeof $text])"
:put "Test of number: $number = $([:typeof $number])"
}

storing global variables
Test of text: my text = str
Test of number: 2345 = num

Second script, using data
{
:put "using global variables"
:global text
:global number
:put "Test of text: $text = $([:typeof $text])"
:put "Test of number: $number = $([:typeof $number])"
}

using global variables
Test of text: my text = str
Test of number: 2345 = num
Nothing changes, string is str and number is num.


Edit test your script.
admin@941-2nD] > :global X1 123
[admin@941-2nD] >
[admin@941-2nD] > {
{... :local i 1;              # set local variable i=1
{... :execute ":global X$i";  # declare global variable X1, since i=1
{... :put [:typeof $X1];      # Test what type of data X1
{... }
num
[admin@941-2nD] >
[admin@941-2nD] > :global X1 "test"
[admin@941-2nD] >
[admin@941-2nD] > {
{... :local i 1;              # set local variable i=1
{... :execute ":global X$i";  # declare global variable X1, since i=1
{... :put [:typeof $X1];      # Test what type of data X1
{... }
str
.
Use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
User avatar
iperezandres
newbie
Topic Author
Posts: 30
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Re: Type "nothing"

Sun Dec 09, 2018 3:13 pm

It works from the terminal but it does not work from a script.
This code in the terminar returns "type=num":
[admin@MK] > {
{... :local i 1;              
{... :execute ":global TrafWAN$i";
{... :global type [:typeof $TrafWAN1];
{... }   
But the same code in a script returns "type=nothing":
:local i 1;
:execute ":global TrafWAN$i";
:global type [:typeof $TrafWAN1];
In both cases the variable $TrafWAN1 already existed as num type and value "123".

Why the same code is being treated differently in the terminal than in the script? Shouldn't be the same?
 
User avatar
Jotne
Forum Veteran
Forum Veteran
Posts: 707
Joined: Sat Dec 24, 2016 11:17 am

Re: Type "nothing"

Sun Dec 09, 2018 5:06 pm

I do now understand what you try to do.

Declaring global variable works as it suppose to do.
But i fails when you try to declare it indirectly using a variable.
It may be an other way to do it, will test some.
But this seems to be as support case for MT. Send them an email at support@mikrotik.com
.
Use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
User avatar
iperezandres
newbie
Topic Author
Posts: 30
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Re: Type "nothing"

Sun Dec 09, 2018 5:12 pm

Thank you Jotne, I will keep on trying different approaches and I will try support@mt as well.
Keep you posted!
 
sebastia
Long time Member
Long time Member
Posts: 684
Joined: Tue Oct 12, 2010 3:23 am

Re: Type "nothing"

Sun Dec 09, 2018 5:12 pm

Maybe it's a case of permissions...

Who has set the original variable? With what permissions?
And what permissions does the script have?

see also viewtopic.php?f=9&t=109696&hilit=script ... permission
Last edited by sebastia on Sun Dec 09, 2018 5:20 pm, edited 2 times in total.
 
User avatar
iperezandres
newbie
Topic Author
Posts: 30
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Re: Type "nothing"

Sun Dec 09, 2018 5:19 pm

Don't think that is the problem, I tried as admin with every policy selected, and same result. Unless the terminal runs the code with different permissions; that, I don't know.
 
User avatar
Jotne
Forum Veteran
Forum Veteran
Posts: 707
Joined: Sat Dec 24, 2016 11:17 am

Re: Type "nothing"

Sun Dec 09, 2018 6:49 pm

Possible solution found.
Do not store you data inn to different global variable, but store all in one global array.

Eks
:global x aa,bb,cc,dd,ee,ff
Script test using indirect variable to get array value.
:local i 2;
:global x
:put [:pick $x $i]
run test
cc
Last edited by Jotne on Sun Dec 09, 2018 7:04 pm, edited 1 time in total.
.
Use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
User avatar
iperezandres
newbie
Topic Author
Posts: 30
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Re: Type

Sun Dec 09, 2018 6:57 pm

Mmm, interesting approach! Let me give it a try... Thanks!
Last edited by iperezandres on Mon Dec 10, 2018 1:56 pm, edited 1 time in total.
 
User avatar
Jotne
Forum Veteran
Forum Veteran
Posts: 707
Joined: Sat Dec 24, 2016 11:17 am

Re: Type "nothing"

Sun Dec 09, 2018 7:06 pm

Edit:

You can set a specific field in the array like this:
:set ($myArray->2) 44
Sets 3rd field (fist field=0) to 44

So for example above
:global x aa,bb,cc,dd,ee,ff
Script test
:local i 2
:global x
:put [:pick $x $i]
:set ($x->$i) "QQ"
:put [:pick $x $i]
Result
run test
cc
QQ
.
Use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 
User avatar
Jotne
Forum Veteran
Forum Veteran
Posts: 707
Joined: Sat Dec 24, 2016 11:17 am

Re: Type "nothing"

Sun Dec 09, 2018 8:26 pm

Even closer to your request
:global x {test1=76;test2=276;test3=387}
/system script> :environment print
x={test1=76; test2=276; test3=387}
script test
:local i 2;
:global x
:put ($x->"test$i")
run test
276
Another example using part of the inner variable with an index
:for a from=1 to=[:len $x] do={:put ($x->"test$a")}
76
276
387
.
Use Splunk to monitor your MikroTik Router

MikroTik->Splunk

Who is online

Users browsing this forum: No registered users and 6 guests