Array functions

Completed

When you work with an array data type, a number of functions can be applied to that data type. These functions can be used to determine the length of an array or to copy an entire or part of an array:

  • ArrayLen

  • CompressArray

  • CopyArray

ArrayLen function

The ArrayLen function returns the number of current elements in an array.

Length := ArrayLen(Array [,Dimension]);

The following example shows an ArrayLen function:

SaleAmount: array[10] of Integer;

Length := ArrayLen(SaleAmount);
Message('%1', Length);
// Displays : 0

SaleAmount[1] := 1;
SaleAmount[2] := 2;
SaleAmount[3] := 3;
SaleAmount[1] := 10;

Length := ArrayLen (SaleAmount);
Message('%1', Length);
// Displays : 3

CompressArray function

The CompressArray function moves all non-empty strings in an array to the beginning of the array. The resulting array has the same number of elements as the input array, but empty entries appear at the end of the entry.

The StringArray parameter is an array of strings.

Count := CompressArray(StringArray);

The following example shows the StringArray parameter:

MyArray: array[4] of Text[20];

MyArray[1] := 'Paris';
MyArray[2] := 'Rome';
MyArray[3] := '';
MyArray[4] := 'New York City';

CompressArray(MyArray);

/* Results :

   MyArray[1] = 'Paris';
   MyArray[2] = 'Rome';
   MyArray[3] = 'New York City';
   MyArray[4] = ''; 

*/

CopyArray function

The CopyArray function will create a new array based on an existing one. You can provide a starting position and, optionally, a length. If you don't provide a length, the function will copy all elements until the end, starting from the position.

CopyArray(NewArray, Array, Position [, Length]);

The following example shows the CopyArray function:

MyArray1: array[10] of Integer;
MyArray2: array[5] of Integer;

MyArray1[1] := 1;
MyArray1[2] := 2;
MyArray1[3] := 3;
MyArray1[4] := 4;
MyArray1[5] := 5;
MyArray1[6] := 6;
MyArray1[7] := 7;
MyArray1[8] := 8;
MyArray1[9] := 9;
MyArray1[10] := 10;

CopyArray(MyArray2, MyArray1, 6, 5);

/* Results :

   MyArray2[1] = 6;
   MyArray2[2] = 7;
   MyArray2[3] = 8;
   MyArray2[4] = 9;
   MyArray2[5] = 10;

*/