Array

One-dimensional array

Arrays are variables that can store more than one value. The data type is array. An array can be likened to a string because strings also contain several values (i.e. characters). The characters are ordered in sequence in the string and they can be accessed through an index.

Arrays can also be used as literals in TDM Studio. In this case, the field elements are enclosed in square brackets:

array a = [11, 22, 33];

This defines an array which contains three elements: the numbers 11, 22, and 33.

The array can be output as a whole or element by element:

1 Trace(TRACELEVEL_INFO, a);

2 Trace(TRACELEVEL_INFO, a[0]);

3 Trace(TRACELEVEL_INFO, a[1]);

4 Trace(TRACELEVEL_INFO, a[2]);

These lines return the following output:

[11, 22, 33]

11

22

33

You can also generate an array that contains strings:

1 array a = ["First", "Second", "Third"];

2

3 Trace(TRACELEVEL_INFO, a);

4 Trace(TRACELEVEL_INFO, a[0]);

5 Trace(TRACELEVEL_INFO, a[1]);

6 Trace(TRACELEVEL_INFO, a[2]);

The result is:

[First, Second, Third]

First

Second

Third

Arrays can store elements of different data types:

1 array a = ["First", 2, 3.8];

2

3 Trace(TRACELEVEL_INFO, a);

4 Trace(TRACELEVEL_INFO, a[0]);

5 Trace(TRACELEVEL_INFO, a[1]);

6 Trace(TRACELEVEL_INFO, a[2]);

The output is:

[First, 2, 3.8]

First

2

3.8

Storing different data types in an array can easily lead to errors, because you have to know exactly which type applies to which element.

Recommendation: Whenever possible, you should make sure that fields only contain elements of one type.

This raises the question why TDM Studio doesn't automatically enforce that arrays may only have elements of one data type.

The reason for this has to do with the ability of running database queries. Since a returned line will normally contain different data types and a line is returned in an array, it has to be possible to accommodate the different types in one array. And you would normally know in a query like this, which elements contains which type.

If you are unsure of the type, you can call the function IsOfType(). This returns the value TRUE for a given value or variable, if it matches the specified type. Otherwise, FALSE is returned.

1 array a = ["First", 2, 3.8];

2

3 Trace(TRACELEVEL_INFO, IsOfType(123, TYPE_INTEGER));

4 Trace(TRACELEVEL_INFO, IsOfType(123, TYPE_FLOAT));

5 Trace(TRACELEVEL_INFO, IsOfType(a[0], TYPE_STRING));

This returns the following output:

True

False

True

Arrays are dynamic up to a point, i.e. their size can change. An array that was created with three elements can also store a fourth or a fifth element. Or you can remove elements from arrays:

1 array a = ["First", 2, 3.8, 44];

2

3 Trace(TRACELEVEL_INFO, Insert(a, 1, [123, 456]));

4 Trace(TRACELEVEL_INFO, Remove(a, 1, 2));

The output looks as follows:

[First, [123, 456], 2, 3.8, 44]

[First, 44]

The array itself (here a) is not changed by Insert() and Remove(), but these functions return their result as a new array instead. This is the same behaviour as Insert() and Remove() for strings.

We did state above that arrays are dynamic "up to a point". What this means is that array will not change their size if the index is out of range:

1 array a = ["First"]; // <-- An element - max. index is 0.

2 a[1] = "Second"; // <-- Error!

If you want to create an array that already contains a certain number of elements of a certain type, you can use the function CreateArray():

array a = CreateArray(10, 0);

This line creates an array with 10 elements which all have the type integer and which are initialized with the value 0. If you want to generate an array of floating comma numbers, you enter 0.0 instead of 0:

array a = CreateArray(10, 0.0);

In the output, the difference will not be visible, though, because for a decimal value of zero, no decimal places are output by default.

Multi-dimensional array

An array that contains other array is described as "multi-dimensional". The following lines generate an array of this kind:

1 array a = [[1, 2], [3, 4], [5, 6]];

2

3 Trace(TRACELEVEL_INFO, a);

4 Trace(TRACELEVEL_INFO, a[0][0]);

5 Trace(TRACELEVEL_INFO, a[1][0]);

6 Trace(TRACELEVEL_INFO, a[2][1]);

This yields the following matrix:

0

1

 

0

1

2

 

1

3

4

 

2

5

6

The array dimension has 3 elements (here they are 3 rows).

Each element in turn contains an array of 2 elements (here they are 2 columns).

Each level (dimension) requires their own pair of brackets, if you want to access the value contained in it. The first bracket is required for the containing array and the second for the contained array. This means that the lines above return the following output:

[[1, 2], [3, 4], [5, 6]]

1

3

6

These are the values from (Row;Column) 0;0, 1;0 and 2;1.