Код: Выделить всё
# Usage: [$arrayPush <$array name> <value> <key position to place value (0-n or -1)>]
# Input an array name, value, and the key position to push the value to. To push value to the end of the array, enter -1.
# If array doesn't already exist, you must declare the variable and set it to "" before calling the function.
:global arrayPush do={
:if ($2="") do={ :error "You did not specify a value to add to the array."; }
:if ($1!="" && $3="") do={ :error "You did not specify the key position to place the value in the array."; }
:if (($3<(-1)) || (([:typeof $3]!="num") && ([:typeof $3]!="str")) || ($3>([:len $1]-1))) do={ :error "Argument 2 invalid."; }
:local string;
:if ([:typeof $1]="array" && $3=(-1)) do={
:foreach item in=$1 do={ :set string ($string . "," . $item); }
:set string ($string . "," . $2);
:set string [:toarray $string];
:return $string;
}
:if ([:typeof $1]="array" && [:len $1]>="0") do={
:local counter "0";
:foreach item in=$1 do={
:if ($counter!=[:tonum $3]) do={
:set string ($string . "," . $item);
:set counter ($counter+1);
} else={
:set string ($string . "," . $2);
:set string ($string . "," . $item);
:set counter ($counter+1);
}
}
:set string [:toarray $string];
:return $string;
}
:if ($1="") do={
:set string $2;
:set string [:toarray $string];
:return $string;
}
}
Код: Выделить всё
# 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]);
};