Community discussions

MikroTik App
 
fibadmin
just joined
Topic Author
Posts: 4
Joined: Mon Jan 15, 2018 3:29 pm

array variable defined as local

Mon Jan 15, 2018 4:02 pm

Hi,
I am programming a script that collects some values from the wireless interface periodically (every 1s during 60s) and stores them in local arrays (mean, max, min) defined in the script.
At the end of the script I copy the local arrays to global variables, making them avaiIable to other scripts, and the scheduler launches my script again.

I found that when doing
:set $global_array $local_array
The global_array becomes a pointer to the local _array, and therefore after the first run of the script, whenever I write on the local_array, the global_array is also updated.
That was annoying and I presumed that I could solve it by converting to string the local_array when I copied it to the global_array:
:set $global_str [:tostr ($local_array)]

The global_str is now static, as I wanted, but I am surprised to see that the local_var is kept after every run of the script, even if I initialise it at the beginning of the script.

Here is a code that reproduces the problem:

# beginning of script
:local $bbb {0;0};
:set ($bbb->1) (($bbb->1)+1);
:log info message="$bbb";
# end of script

If I run the code several times, $bbb becomes:
0;1
0;2
0;3
...

So, I conclude that the code [:local bbb {0;0}] does not initialize the array to 0;0 and moreover the arrays are not local, as they maintain their values in subsequent executions of the script.
I would be grateful to receive an explanation.
Thank you in advance.
 
User avatar
abbbbbas1
just joined
Posts: 3
Joined: Mon Nov 09, 2020 2:53 am
Location: iraq
Contact:

Re: array variable defined as local

Tue Mar 02, 2021 4:59 pm

Can you help with some of the things? I would like to copy the numbers found in the search for the right soldier in the air with less noise, reduce them in an array, and copy the array to switching generals.
 
Emil66
Frequent Visitor
Frequent Visitor
Posts: 62
Joined: Tue Aug 28, 2018 2:09 am

Re: array variable defined as local

Tue Mar 02, 2021 10:32 pm

See the explanation and a workaround here: viewtopic.php?f=9&t=172125#p843397

Who is online

Users browsing this forum: rextended and 21 guests