Community discussions

MikroTik App
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 539
Joined: Wed Mar 16, 2016 6:12 am

Scripting Array help

Fri Nov 09, 2018 2:10 am

The official help https://wiki.mikrotik.com/wiki/Manual:S ... ith_Arrays is not very helpful when it comes to understanding and using arrays
There needs to be more examples and theory explanation


What i'm trying to do is something like this

:global SetInterfaces {"ether1", "ether2", "ether5"}
:foreach i in=$SetInterfaces do={/interface set [find where name=$i] comment="Mark This Interface"}

I imagine that command should run 3 times, setting each interface specified in the array with the comment of "Mark This Interface" but it doesn't work because when I change the script to this
:global SetInterfaces {"ether1", "ether2", "ether5"}
:foreach i in=$SetInterfaces do={:put $i}
The output is "ether1;ether2;ether5" implying the command runs only once and outputs every array value at the same time
Instead I expect the output should have been
ether1
ether2
ether5
so that it runs 3 times
As a result the first example fails because its only running once and looking for an interface called "ether1;ether2;ether5" instead of running 3 times looking for "ether1" then "ether2" then "ether5"

So my question is how do I achieve the desired result? Keep in mind the length of the array is dynamic, it might only have 3 interfaces now but its not always going to be the case, sometimes only 1 interface, sometimes 20 interfaces or anything in between
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3300
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Scripting Array help

Fri Nov 09, 2018 10:24 am

Try to change , with ;
:global SetInterfaces {"ether1"; "ether2"; "ether5"}
:foreach i in=$SetInterfaces do={:put $i}
It these are not needed in other places you can use local variable.
:local SetInterfaces {"ether1"; "ether2"; "ether5"}
:foreach i in=$SetInterfaces do={:put $i}
To test it out wrap all in {} and cut and past it to command line window like this:
{
:local SetInterfaces {"ether1"; "ether2"; "ether5"}
:foreach i in=$SetInterfaces do={:put $i}
}

PS use code tags on your codes, makes it more easy to read.
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 539
Joined: Wed Mar 16, 2016 6:12 am

Re: Scripting Array help

Tue Nov 20, 2018 1:11 am

I managed to get this working. Yes need to use ; instead of , in arrays
Also, need to use :for not :foreach. Foreach runs all instances at the same time, for runs one time for each entry separately. Here's a working example that pulls out all values in an array of any length

:global SetInterfaces {"ether1"; "ether2"; "ether5"; "wlan1"}
:for i from=0 to=([:len $SetInterfaces]-1) do={:put [:pick $SetInterfaces $i]}

I know I can use local but global is much easier for troubleshooting. Having to wrap everything in {} means it's not possible to up arrow and simply repeat a command when debugging, plus global will hold the values so I don't have to keep repeating the :local statement. After I change it to a script I can use local variable
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3300
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: Scripting Array help

Tue Nov 20, 2018 9:04 am

If you use Global value, you can later delete them here:
/system script environment
PS please use code tags </> button for your codes.
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: Scripting Array help

Fri Nov 23, 2018 5:05 am

Who is online

Users browsing this forum: No registered users and 60 guests