I created this simple array push function but have ran into some issues. The function works fine as long as long as the array name that you pass it as the first argument already exists, the variable type equals array, and the value is not null. I would like to be able to call this function in cases where if the array doesn’t already exist, to create it, and add the second parameter to it. The problem with this is since the array doesn’t exist, therefore it’s value equals nil and it is basically like you never added the first parameter to the function which produces wacky results.
Here is the function
Usage: [$arrayPush ]
Input an array name and a value to push to the end of the array
:global arrayPush do={
:if ($1=“”) do={ :error “You did not specify an array name argument.”; }
:if ($2=“”) do={ :error “You did not specify a value to add to the array.”; }
:local string;
:if ([:typeof $1]=“array”) do={
:foreach item in=$1 do={ :set string ($string . “,” . $item); }
:set string ($string . “,” . $2);
:set string [:toarray $string];
:return $string;
} else={ :error “Argument 1 variable type was not an array.”; }
}Here is an example of what I mean
:global myVar;
:set myVar [:toarray $myVar];
:set myVar [$arrayPush $myVar “some value”];Since $myVar is nil, the toarray function essentially does nothing and the next line has undesired results. I’m not sure exactly how to explain this but hopefully you get the picture. Any ideas on a way to work around this?
The only thing I can currently think of is to test if the array exists before I initially call the function and if not, create a variable as a string, then convert it to an array but that’s a lot of extra steps.
I hope you like this shorter function I made for you:
# Usage: [$arrayXpush <$array name> <value> <key position to place value>]
# <$array name> is one array name, if not exist, or are not provided one array, the function work like are passed one empty array
# <value> can be any type of value, if empty simply return $array back, if $array not exist, return one empty array.
# <key position to place value> key position to push the value to
# if any number < 0, is at beginning
# if any number > length of the array is placed at the end
# if third parameter are one empty string or omitted, the default behavior are to place the value at the array end.
# if third parameter are not one number, the default behavior are to place the value at the array start.
:global arrayXpush do={
:local arrX value=[:toarray $1];
:if ([:len $arrX] = 0) do={ :set $arrX value=[:toarray ""]; };
:local arrXlen value=[:len $arrX];
:local valX value=[:tostr $2];
:if ($valX = "") do={ :return value=$arrX; };
:local posX value=([:tostr $3]);
:if ($posX = "") do={ :set $posX value=($arrXlen + 1); };
:set $posX value=([:tonum $posX] + 0);
:if ($posX < 0) do={ :set $posX value=0; };
:if ($posX > $arrXlen) do={ :set $posX value=$arrXlen; };
:if ($posX = 0) do={ :return value=($valX,$arrX); };
:if ($posX = $arrXlen) do={ :return value=($arrX,$valX); };
:return value=([:pick $arrX 0 ($posX - 1)],$valX,[:pick $arrX ($posX - 1) $arrXlen]);
};
Now, you can try to add this
# if 4th parameter are "R" and the <key position to place value> value are between array limit, the function replace the value, instead of be inserted.
My worry with solutions like this is normally “what is going on under the hood”…
Maybe it is my bad that I started programming 40 years ago and back then the beautiful solution often was too slow to be usable in practice. Of course that has changed a little today, although a lot of the slowness of modern systems can be explained by careless programming by people who do not know about efficiency.
A statement like: :set $MyArray ($MyArray, $Value); looks nice and easy, but I fear that it will copy the entire existing array everytime you add a single value to it.
That could end up disastrous when you have many values in the array.