Community discussions

MikroTik App
 
termers
just joined
Topic Author
Posts: 19
Joined: Fri Jan 20, 2017 2:46 am

Confuse about Global Array Variable

Thu Oct 19, 2017 10:36 am

I have this following code that confuse me...
:global test ({});

:local a $test;

/log info $a;

:set ($test->"what") "yes it is";

/log info $a;
Output :
(blank/empty line)
what=yes it is
based on the output... why is it that $a also have its value changed when the only variable I changed was the $test variable..

next one was I deleted the test global variable on the environment tab... and run it again...
Output:
what=yes it is
what= yes it is
why is it that it outputs twice when I deleted the global variable and already have declared this at top :global test ({}); <-- (which i assumed it will create a new empty array).. can anyone please help me or explain it to me..
 
pe1chl
Forum Guru
Forum Guru
Posts: 10240
Joined: Mon Jun 08, 2015 12:09 pm

Re: Confuse about Global Array Variable

Thu Oct 19, 2017 10:48 am

I think when you set a variable equal to an array variable, it will not copy the array but it will
make the variable a pointer to the same array. Similar to what happens when you pass a function parameter.

I have done some script programming with array and indeed it sometimes behaves a bit weird when you
are used to some other languages. And worst, there are no useful error messages so you can have bad
code without knowing where the error is... especially when it is running from some system event.
 
termers
just joined
Topic Author
Posts: 19
Joined: Fri Jan 20, 2017 2:46 am

Re: Confuse about Global Array Variable

Thu Oct 19, 2017 10:58 am

What you said does make sense to me :D .. I tried it vice versa
:global test ({});

:local a $test;

/log info $test;

:set ($a->"what") "yes it is";

/log info $test;
and it showed the same results... and yea... I am used to other programming language that's why I find it very weird... the pointer thing did not cross my mind :D ... Thanks for the reply :)
 
pe1chl
Forum Guru
Forum Guru
Posts: 10240
Joined: Mon Jun 08, 2015 12:09 pm

Re: Confuse about Global Array Variable

Thu Oct 19, 2017 11:44 am

A trick I discovered by accident while pulling my hair out to find some bug:
When you define a global function like:
:global funcname do={
...
}

and then you display the global variables, you see a "parsed" version of the function in
a stack language, and you can sometimes see problems that you would miss otherwise.
(and it becomes clear why runtime error messages cannot reference the line and character where they occur)
 
termers
just joined
Topic Author
Posts: 19
Joined: Fri Jan 20, 2017 2:46 am

Re: Confuse about Global Array Variable

Thu Oct 19, 2017 12:35 pm

Woah this is very handy!... thnxs for this trick pe1chl 8) .

Who is online

Users browsing this forum: No registered users and 33 guests