Community discussions

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

Help with arrays for shortening scripts?

Mon Jul 31, 2017 3:27 am

I'm still fairly new to the Mikrotik scripting language and I find the wiki not always the easiest and most helpful resource, so i'm hoping can someone can quickly clarify something for me

I have some scripts that run and use fixed variable names, scripts such as checking if an interface is up end-to-end rather than just relying on the first hop. Many of these scripts have multiple sections that are all identical with 1 number changed in the variable name, i.e.......(in pseudo code)
PingResultISP1 = (ping UpCheckISP1 interface=ISP1)
if PingResultISP1 < 1 then (increase route distance ISPRoute1)
PingResultISP2 = (ping UpCheckISP2 interface=ISP2)
if PingResultISP2 < 1 then (increase route distance ISPRoute2)
PingResultISP3 = (ping UpCheckISP3 interface=ISP3)
if PingResultISP3 < 1 then (increase route distance ISPRoute3)
I want to change this to
NumberOfGateways = 3
for n = 1 to 3
   PingResultISP[n] = (ping UpCheckISP[n] interface=ISP[n])
   if PingResultISP[n] < 1 then (increase route distance ISPRoute[n])
next n
Can someone help me out regarding syntax of arrays in the scripting language and the appropriate way to use them?
 
millenium7
Long time Member
Long time Member
Topic Author
Posts: 589
Joined: Wed Mar 16, 2016 6:12 am

Re: Help with arrays for shortening scripts?

Mon Aug 07, 2017 2:47 am

bump
 
ovdeathiam
just joined
Posts: 21
Joined: Wed Jul 16, 2014 11:15 am

Re: Help with arrays for shortening scripts?

Mon Aug 07, 2017 9:00 am

Arrays are comma separated.

Here is an example how you can use them:
:local myarray [:toarray value="abc,def,ghi"];
:put $myarray;

:put ($myarray->0);
:put ($myarray->1);
:put ($myarray->2);

:foreach element in=$myarray do={
    :put $element
}

:for counter from=0 step=1 to=2 do={
    :put ($myarray->$counter)
}
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 3061
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

Re: Help with arrays for shortening scripts?

Mon Aug 07, 2017 10:40 am

Chapter "Operations with Arrays" from https://wiki.mikrotik.com/wiki/Manual:Scripting
viewtopic.php?t=69753

Ask "Uncle Google" for "script mikrotik arrays". You will be amazed how many examples you can find.