Mikrotik Scripting – Array Pop Function

Here is another function I created to delete a specific key in an array.

# 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;
    }
}

Leave a Reply