You are on page 1of 3

Arrays

You use arrays to store multiple variables of the same type within
one data item. Each variable can be referred to with the construction
array_name(index). For example, the first element in a one-
dimensional array is array_name(1).

An array can have up to four dimensions. The dimension (and, in case


of a string, also the length) of an array must be stated in the
declaration.
There are three types of arrays: long, double, and string.

Long arrays

You declare one-dimensional long arrays as follows:

LONG lng_array(5) | 1-dimensional array for 5 longs

The number in parentheses after the array name specifies the number
of longs that can be stored in the array.
You declare two-dimensional long arrays as follows:

LONG lng_array(2,4)

The first number after the array name indicates the number of
dimensions in the array. The second number indicates the number of
longs that can be stored in each dimension. The following diagram
illustrates a two-dimensional array as a matrix:

lng_array(1,1) = 3 lng_array(2,1) =
100lng_array(1,2) = 4 lng_array(2,2) = 98lng_array(1,3) = 56
lng_array(2,3) = 55lng_array(1,4) = 78 lng_array(2,4) = 77
Double arrays

A double array differs from a long array only in that it is prefixed


by the keyword DOUBLE and the array elements must be filled with
double values. You declare a double array as follows:

DOUBLE dbl_array(5)

String arrays

The declaration of string arrays differs from that for other array
type in that the first dimension contains the length of the strings
to be stored in the array. For example:

STRING str_arr(10,5) | 5 strings of length 10

STRING str_arr(10,5,5,5) | A 5 x 5 x 5 matrix of strings of


length 10

When referencing a string array, the first dimension indicates the


start position in the string. The particular string to be accessed is
indicated by the other dimensions. It is possible to specify a length
for the string to be retrieved by using a semicolon [;] followed by
the required length. If you omit the length, the total length of the
string is taken.

Example 1 (two-dimensional string array)


STRING str_arr(6,2) Space reserved for 2 strings of 6 characters
each. For example, the first string contains "abcdef" and the second
"ABCDEF".
str_arr(1,1) = "abcdef" This references the first string starting at
position 1.
str_arr(2,1;1) = "b" This references the first string starting at
position 2 with length 1.
str_arr(3,2) = "CDEF" This references the second string beginning
at position 3.
Example 2 (filling string variables)

Consider a string declared as:

STRING strg(26)

The statements listed below will have the indicated results:

strg = "good morning" The words "good morning" are stored from the
first position of strg:"good morning"
strg(1) = "good morning" The same result but filled with spaces:
"good morning "
strg(9) = "good morning" The string is stored from position 9
onwards and filled with spaces:" good morning "
strg(2;3) = "Hello" The letters "Hel" (3 characters) are stored
from position 2 onwards: " Hel good morning "
Example 3 (accessing string variables)

Suppose the string declared in the previous example has the value
"ABCDEFGHIJKLMNOPQRSTUVWXYZ". The results of the statements below
will be as indicated:

print strg "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


print strg(13) "MNOPQRSTUVWXYZ"

print strg(2;3) "BCD"

In these examples, strg(n) refers to the entire array from position n


onwards and strg(n;x) refers to x positions starting at position n.

Example 4 (two-dimensional string array)

Consider a 2-dimensional string array declared as:

STRING twice(6,2)

The statements below will have the indicated results.

twice(1,1) = "ABCDEF" The first string is filled from position 1


onwards.
twice(1,2) = "123456" The second string is filled from position 1
onwards.
print twice(3,1) "CDEF" is printed.
print twice(4,1;2) Two characters are printed starting at
position 4 in string 1 – that is, "DE" is printed.
String lengths

There are two lengths associated with a string: the maximum and the
current length. The maximum length is defined at declaration of the
string and the current length can vary, depending on the contents of
the string.
If a string is declared as STRING strg(100), the maximum length of
strg is 100. If strg is filled as strg = "hello", the current length
is 5. If strg is filled as strg(1) = "hello", the current length is
also is 100, as the remaining positions are filled with spaces. When
you specify a start position, the string is always filled out with
spaces.

If a string is declared as FIXED, the current length is always equal


to the maximum length, because the string is always filled up with
spaces. Also, in the case of multi-dimensional strings, the current
length is always equal to the maximum length.

Related topics

§ 3GL programming language: overview

§ Variables

© 1998 Baan Development B.V. All rights reserved

You might also like