Tech Tip: Entering an object representing an array of values in a collection
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: December 14, 2017
The New collection command does not have the ability to input an array as a value. A way to achieve this is to convert the array to an object. The follow utility method will convert arrays to an object so it can be added to the New collection:
// -------------------------------------------------------------- // Method: ARRAY_TO_4D_OBJECT // Description: Method convert an a given array to an 4D Object // in order to be inserted to the new collection command // // Parameters: // $1 (POINTER) - Pointer to array of LongInt, Integer, Real // Date, Text, Boolean, Pointer, and Object. // Output: // $0 (OBJECT) - Return an object as the property is array index // Example #1 Array LongInt: // [33,232,8] -> {1:33, 2:232, 3: 8} // Example #2 Array Text: // ["a","b","c"] -> {1:"a", 2:"b", 3: "c"} // ---------------------------------------------------------------- C_POINTER($1;$arr) C_OBJECT($0;$obj) C_LONGINT($i) $arr:=$1 If ((Type($arr->)=Integer array) | (Type($arr->)=LongInt array) | \ (Type($arr->)=Real array) | (Type($arr->)=Time array) | \ (Type($arr->)=Date array) | (Type($arr->)=Boolean array) | \ (Type($arr->)=Text array) | (Type($arr->)=Pointer array) | \ (Type($arr->)=Object array)) For ($i;1;size of array($arr->)) OB SET($obj;String($i);$arr->{$i}) End for End if $0:=$obj |
Example #1 of inserting an ARRAY LONGINT:
C_COLLECTION($myCollection) C_OBJECT($newObj) ARRAY LONGINT(arr;0) APPEND TO ARRAY(arr;20) APPEND TO ARRAY(arr;40) APPEND TO ARRAY(arr;60) $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |
Example #2 of inserting an ARRAY TEXT:
ARRAY TEXT(arr;0) APPEND TO ARRAY(arr;"A") APPEND TO ARRAY(arr;"B") APPEND TO ARRAY(arr;"C") $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |
Example #3 of inserting an ARRAY BOOLEAN:
ARRAY BOOLEAN(arr;0) APPEND TO ARRAY(arr;false) APPEND TO ARRAY(arr;false) APPEND TO ARRAY(arr;true) $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |
Example #4 of inserting an ARRAY TIME:
ARRAY TIME(arr;0) APPEND TO ARRAY(arr;Current time) APPEND TO ARRAY(arr;Current time+1) APPEND TO ARRAY(arr;Current time+2) $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |
Example #5 of inserting an ARRAY DATE:
ARRAY DATE(arr;0) APPEND TO ARRAY(arr;current date) APPEND TO ARRAY(arr;current date+1) APPEND TO ARRAY(arr;current date+2) $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |
Example #6 of inserting an ARRAY POINTER:
ARRAY POINTER(arr;0) ARRAY LONGINT(arrLongInt;0) C_LONGINT($num) C_TEXT($text) $num:=7 $text:="seven" APPEND TO ARRAY(arrLongInt;8) APPEND TO ARRAY(arrLongInt;23) APPEND TO ARRAY(arrLongInt;45) APPEND TO ARRAY(arr;->$num) APPEND TO ARRAY(arr;->$text) APPEND TO ARRAY(arr;->arrLongInt) $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |
Example #7 of inserting an ARRAY OBJECT:
ARRAY OBJECT(arr;0) C_OBJECT($obj1;$obj2;$subObj2;$obj3;$subObj3;$subsubObj3) OB SET($obj1;"Company";"Harvey's Fabric Inc.") OB SET($subObj2;"FirstName";"Harvey") OB SET($obj2;"Employee";$subObj2) OB SET($subsubObj3;"FirstName";"Harvey") OB SET($subObj3;"Employee";$subsubObj3) OB SET($obj3;"Company";$subObj3) APPEND TO ARRAY(arr;$obj1) APPEND TO ARRAY(arr;$obj2) APPEND TO ARRAY(arr;$obj3) $newObj:=ARRAY_TO_4D_OBJECT (->arr) $myCollection:=New collection($newObj) |