Arrays are declared by using the Array type name, optionally followed by "of" and the type of the elements. If the element type is omitted, it is set by default to ANY.
Local Array of Number &MYARRAY; Local Array &ARRAYANY;
Arrays can be composed of any valid PeopleCode data type, such as string, record, number, date, and so on.
PeopleSoft recommends you declare every object you use in PeopleCode. This provides some syntax checking when you save PeopleCode. It’s better to find out that you misspelled the name of a method or property at design time, rather than at runtime!
Arrays can be declared as Local, Global, or Component, just like any other PeopleTools object.
Arrays can be created with one or more dimensions. An array with more than one dimension is called an array of arrays. The dimension of an array is the number of Array type names in the declaration. This is also called the depth of an array. The maximum depth of a PeopleCode array is 15 dimensions.
In the following example, &MYARRAY has three dimensions, and &MYA2 has two dimensions.
Local Array of Array of Array of Number &MYARRAY; Local Array of Array &MYA2;
An array must always have a consistent dimension. This means that in a one-dimensional array none of the elements can be an array, in a two-dimensional array all of the elements must be one-dimensional arrays, and so on.
After you declare an array, use one of the built-in array functions to instantiate it and return an object reference to it. For example, the following creates an array containing one element of type &TEMP, whatever data type &TEMP may be.
&MYARRAY = CreateArray(&TEMP);
Or you can use the CreateArrayRept function to instantiate
an array. The Rept stands for repeat. CreateArrayRept creates an array that contains the number of copies
you specify of a particular value. The following code creates an array
with three copies of the string &MYSTRING. This does not create a three-dimensional array, but
rather creates an array that’s already populated with three elements
of data (
Len = 3), each of which contain
the same string (&MYSTRING).
&MYARRAY = CreateArrayRept(&MYSTRING, 3);
An array object can be assigned to an array variable. Array objects can be passed from and returned to any kind of PeopleCode function:
ANewFunc (&myarray); MyFunc (&myarray); &MyArray = YourFunc("something");
For example, the ReturnToServer function returns an array of nodes to which a message can be published.
Elements in an array are specified by providing a bracketed subscript after the array object reference:
&MyArray = 123; &temp = &memory; &temp = &memory[1, 2, 3]; /* Same as preceding line. */ MyFunc(&MyArray); MyFunc(10) = "a string";
To access data in a two-dimensional array, you must specify both indexes. The following accesses the second item in the first subarray:
&VALUE = &DOUBLE;
You receive an error if you use a zero or negative index in an array. Accessing an array element whose index is larger than the last array element is also an error, but storing to such an index extends the array. Any intervening elements between the former last element and the new last element are assigned a value based on the element type of the array. This is the same value as an unassigned variable of that type.
An array is an object, which means that assignments to an array are the same as for any other object. An array variable can be assigned the distinguished value NULL, which indicates the absence of any array value.
Array variables are supported for all scopes. This means that you can have local, global, and Component array variables.