Community discussions

MikroTik App
 
royalpublishing
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Mon Sep 23, 2013 5:47 pm

Array Pop Function

Mon Jun 16, 2014 6:47 pm

Here's another array function I created for scripting...

ros code

# Usage: [$arrayPop <$array name> <key position to delete (0-n or -1)>]
# Input an array name and the integer of the key to delete. To delete the last element of the array, enter -1.
:global arrayPop do={
    :if ($1="") do={ :error "You did not specify an array name argument."; }
    :if ([:typeof $1]!="array") do={ :error "Argument 1 variable was not an \"array\"."; }
    :if ($2="") do={ :error "You did not specify a key to delete from the array."; }
    :if (($2<(-1)) || (([:typeof $2]!="num") && ([:typeof $2]!="str")) || ($2>([:len $1]-1))) do={ :error "Argument 2 invalid."; }
    :local string;
    :if ($2=(-1)) do={ 
        :for i from=0 to=([:len $1]-1) do={
            :if ($i!=([:len $1]-1)) do={ :set string ($string . "," . [:pick $1 $i]); }
        }
        :set string [:toarray $string];
        :return $string;
    } else={ 
        :for i from=0 to=([:len $1]-1) do={
            :if ($i!=[:tonum $2]) do={ :set string ($string . "," . [:pick $1 $i]); }
        }
        :set string [:toarray $string];
        :return $string;
    }
}
 
User avatar
semakka
Member Candidate
Member Candidate
Posts: 196
Joined: Mon Sep 11, 2006 10:59 am
Location: Moraira, Alicante, Spain
Contact:

Re: Array Pop Function

Tue Jun 17, 2014 11:11 pm

nice. what does it do? :)
 
royalpublishing
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 50
Joined: Mon Sep 23, 2013 5:47 pm

Re: Array Pop Function

Tue Jun 17, 2014 11:38 pm

In a nutshell, it will put your router into an endless loop resulting in a subsequent crash, followed by a blazing fireball of hot ash and will ultimately bring on both the death and reincarnation of RouterOS in animal form...or was that a legitimate question?

If you have an array such as:

ros code

:local myVar ("cat","dog","horse","monkey");
      key       0     1      2        3
The function can either delete any specific entry out of the array and then return the new array minus that key or simply return the array minus last key.
 
User avatar
semakka
Member Candidate
Member Candidate
Posts: 196
Joined: Mon Sep 11, 2006 10:59 am
Location: Moraira, Alicante, Spain
Contact:

Re: Array Pop Function

Tue Jun 17, 2014 11:42 pm

now, I can see the light at the end of the tunnel :)