KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to insert specified value into an array
PRODUCT: 4D | VERSION: 15.1 | PLATFORM: Mac & Win
Published On: March 25, 2016

Below is an utility method to insert a certain value into a specified location of an array. The value at the current specified location, as well as values after it will be shifted. If the location is larger than the size of the array, the value will be inserted to at the end of the array.

//Method: INSERT_VAL_IN_ARRAY
//$1 - pointer to the array
//$2 - where to insert value
//$3 - value to insert
C_POINTER($1;$3;$arr;$val)
C_LONGINT($2;$element;$size)
If (Count parameters>=3)
   $arr:=$1
   $element:=$2
   $val:=$3
   $size:=Size of array($arr->)
   Case of
      : ($element>$size)
      $element:=$size+1
      : ($element<0)
      $element:=0
   End case
   INSERT IN ARRAY($arr->;$element)
   $arr->{$element}:=$val->
End if


Below is an example of using the utility method above to insert a value into an array at a specified location.
ARRAY LONGINT($arr;0)
APPEND TO ARRAY($arr;1)
APPEND TO ARRAY($arr;2)
APPEND TO ARRAY($arr;3)
APPEND TO ARRAY($arr;4)

$location:=3
$val:=22222
INSERT_VAL_IN_ARRAY(->$arr;$location;$val)


Results: