KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to copy an array into a 2D array
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: February 21, 2003

Compatibility: Version 6.7.x and 6.8.x

A 2D array is in fact an array of arrays that you define through a statement such as:
ARRAY LONGINT(MyArray;5;4).

This means that we created an array with 5 elements and each element contains 4 elements. A graphical representaiton would be a matrix. Each first element may not contain elements.

For example, ARRAY LONGINT(MyArray;5;0) means that I have an array with 5 elements but each element does not contain any elements.

In order to add an element to this 2D array, you need to use the INSERT ELEMENT command. This will insert a new item inside your array based on how the 2D array has been declared. For example, the following code will do:

ARRAY LONGINT(TheArray;3;0)
INSERT ELEMENT(TheArray;1;1)
--> It will insert a first element but empty.

while
ARRAY LONGINT(TheArray;3;4)
INSERT ELEMENT(TheArray;1;1)
--> It will insert a first element that will already contain 4 items.

Once the element has been inserted, you can copy an array, redefine the size of the current element, copy each item one by one, or just use the COPY ARRAY command.

1. You can use the INSERT ELEMENT command to insert additional elements:

ARRAY LONGINT(TheArray;3;0)
INSERT ELEMENT(TheArray;1;1)
INSERT ELEMENT (TheArray{1};5;1)

This code will insert 5 elements to our first array element, i.e. TheArray{1}.
Then, we can fill TheArray{1} programatically.

2. You can also use the COPY ARRAY command.

` $ATempArray is an array that contains 9 elements
ARRAY LONGINT(TheArray;3;0)
INSERT ELEMENT(TheArray;1;1)
COPY ARRAY($ATempArray;TheArray{1})

TheArray{1} element will contain 9 elements, copied from $ATempArray, since COPY ARRAY resizes and replaces the contents of the destination array based on the source array.