Array Constants


Array constant syntax allows you to define a constant array of elements that can be used as a function parameter or to initialize an array variable. Array constants may be used to declare a numeric, string or object array.

There are two types of array constants. The first type encloses dimensions with square brackets [1,2,3] and the second encloses dimensions with soft braces {1,2,3}. While these two types of array constants look almost identical, they create arrays in the reverse order from one another. Square brackets and soft braces are only used for constant array declarations.

a() = [1,2,3]

The statement above would initialize a() to a 3 element one dimensional array.

For one dimensional arrays braces work the same as brackets and can be used interchangeably

a() = {1,2,3}


It's when you go beyond one dimension that the difference becomes apparent.

a() = [[1,2,3],[4,5,6]]

1

4

2

5

3

6

b() = {{1,2,3},{4,5,6}}

1

2

3

4

5

6

When you use Brackets as above a(), each matching pair of brackets encloses a dimension with the innermost pair corresponding to the FIRST array index. It is this innermost pair of brackets [] that contain the values and it is the first dimension that varies fastest. In the second example above b() uses braces {} with the innermost pair enclosing values corresponding to the LAST index. The above examples create two different size arrays equivalent to the code below.

dim a(2,1)
dim b(1,2)

for y = 0 to 1
for x = 0 to 2
a(x,y) = n(i)
b(y,x) = n(i)
i = i + 1
next
next

In two dimensions the following statements are equivalent, declaring the rows of a matrix in the order a(row,column).

a() = {
{0, 1, 2, 3} ,
{4, 5, 6, 7} ,
{8, 9, 10, 11}
}

a() = MatTranspose([
[0, 1, 2, 3] ,
[4, 5, 6, 7] ,
[8, 9, 10, 11]
])

Array constants may span multiple lines.

a() = [[[1,2,3],[4,5,6]],
[[7,8,9],[0,1,2]]]

You can always tell how many dimensions an array constant has by counting the leading open braces/brackets. In the example above there are three open brackets together at the front so there are three dimensions.

When using an array constant to initialize an array following a dim, the array dimensions must match.

dim a(2,1) = [[1,2,3],[4,5,6]]

When initializing an entire array with an assignment statement, the array is dynamically re-dimensioned to the size of the array constant.

a() = [[1,2,3],[4,5,6]]







Become a Patron