You declare an array of structure items when you specify that a structure item in a record or form part has an occurs value greater than one, as in the following example:
Record myRecordPart 10 mySI CHAR(1)[3]; end
If a record called myRecord is based on that part, the symbol myRecord.mySi refers to a one-dimensional array of three elements, each a character.
You can reference an entire array of structure items (for example, myRecord.mySi) in these contexts:
An array element that is not itself an array is an item like any other, and you can reference that item in various ways; for example, in an assignment statement or as an argument in a function invocation.
You can refer to an element of a one-dimensional array like myRecord.mySi by using the name of the array followed by a bracketed subscript. The subscript is either an integer or an item that resolves to an integer; for example, you can refer to the second element of the example array as myStruct.mySi[2]. The subscript can vary from 1 to the occurs value of the structure item, and a run-time error occurs if the subscript is outside of that range.
If you use the name of a structure-item array in a context that requires an item but do not specify a bracketed subscript, EGL assumes that you are referring to the first element of the array, but only if you are in VisualAge Generator compatibility mode. It is recommended that you identify each element explicitly. If you are not in VisualAge Generator compatibility mode, you are required to identify each element explicitly.
The next examples show how to refer to elements in a one-dimensional array. In those examples, valueOne resolves to 1 and valueTwo resolves to 2:
// these refer to the first of three elements: myRecord.mySi[valueOne] myRecord.mySi // not recommended; and valid // only if VisualAge Generator // compatibility is in effect // this refers to the second element: myRecord.mySi[valueTwo]
A one-dimensional array may be substructured, as in this example:
record myRecord01Part 10 name[3]; 20 firstOne CHAR(20); 20 midOne CHAR(20); 20 lastOne CHAR(20); end
If a record called myRecord01 is based on the previous part, the symbol myRecord01.name refers to a one-dimensional array of three elements, each of which has 60 characters, and the length of myRecord01 is 180.
You may refer to each element in myRecord01.name without reference to the substructure; for example, myRecord01.name[2] refers to the second element. You also may refer to a substructure within an element. If uniqueness rules are satisfied, for example, you can reference the last 20 characters of the second element in any of the following ways:
myRecord01.name.lastOne[2] myRecord01.lastOne[2] lastOne[2]
The last two are valid only if the program, page-handler, or library property allowUnqualifiedItemReferences is set to yes.
For details on the different kinds of references, see References to variables and constants.
If a structure item with an occurs value greater than one is substructured and if a subordinate structure item also has an occurs value greater than one, the subordinate structure item declares an array with an additional dimension.
Let's consider another record part:
record myRecord02Part 10 siTop[3]; 20 siNext CHAR(20)[2]; end
If a record called myRecord02 is based on that part, each element of the one-dimensional array myRecord02.siTop is itself a one-dimensional array. For example, you can refer to the second of the three subordinate one-dimensional arrays as myRecord02.siTop[2]. The structure item siNext declares a two-dimensional array, and you can refer to an element of that array by a syntax like this:
// row 1, column 2 myRecord02.siTop.siNext[1,2]
To clarify what area of memory is being referenced, let's consider how data in a multidimensional array is stored. In the current example, myRecord02 constitutes 120 bytes. The referenced area is divided into a one-dimensional array of three elements, each 40 bytes:
siTop[1] siTop[2] siTop[3]
Each element of the one-dimensional array is further subdivided into an array of two elements, each 20 bytes, in the same area of memory:
siNext[1,1] siNext[1,2] siNext[2,1] siNext[2,2] siNext[3,1] siNext[3,2]
A two-dimensional array is stored in row-major order. One implication is that if you initialize an array in a double while loop, you get better performance by processing the columns in one row before processing the columns in a second:
// i, j, myTopOccurs, and myNextOccurs are data items; // myRecord02 is a record; and // sysLib.size() returns the occurs value of a structure item. i = 1; j = 1; myTopOccurs = sysLib.size(myRecord02.siTop); myNextOccurs = sysLib.size(myRecord02.siTop.siNext); while (i <= myTopOccurs) while (j <= myNextOccurs) myRecord02.siTop.siNext[i,j] = "abc"; j = j + 1; end i = i + 1; end
You must specify a value for each dimension of a multidimensional array. The reference myRecord02.siTop.siNext[1], for example, is not valid for a 2-dimensional array.
An example declaration of a 3-dimensional array is as follows:
record myRecord03Part 10 siTop[3]; 20 siNext[2]; 30 siLast CHAR(20)[5]; end
If a record called myRecord03 is based on that part and if uniqueness rules are satisified, you can reference the last element in the array in any of the following ways:
myRecord03.siTop.siNext.siLast[3,2,5] myRecord03.siLast[3,2,5] siLast[3,2,5]
In general terms, you can reference an element of a multidimensional array by placing subscripts at the end of the reference:
For details on the different ways to reference a structure item, see References to variables and constants.
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.