PeopleCode | Creating and accessing a two-dimensional array in PeopleCode
A. A Mini Temp Table
Arrays were introduced in Tip 013 with its simplest form, the one-dimensional array. We now move to the next level, that of two-dimensional arrays. As noted in Tip 013, arrays can go up to fifteen dimensions in PeopleCode. We will stick to two dimensions for now.
Two-dimensional arrays are easy to visualise. A standard SQL table is a form of two-dimensional array, with rows and columns acting as dimensions. A two-dimensional array in PeopleCode can therefore act as a mini temp table, one that allows you to store data in a grid-like structure.
This means a two-dimensional array is useful when you wish to keep a small set of data in working memory, without having to set up a temporary table in the database (and potentially annoying a DBA). A two-dimensional array can also be accessed much quicker than a standard SQL table. And finally, as we’ll see below, there is a handy method called ‘Join’, that allows you to easily see the contents of the entire array.
As an example to work through, let’s say we have a requirement to store a list of Employee IDs, along with a process status (either ‘Y’ for processed, ‘N’ for not processed, or ‘E’ processed with errors). The data would appear as follows:
This requirement could be met by creating a two-dimensional array, as described below. The process of creating and accessing the array is broadly similar to that described in Tip 013.
B. Defining the Array
We start by declaring two versions of the array, a one-dimensional version for a single Employee ID, and then the two-dimensional version for all the employees:
Local array of string &singleEmp; Local array of array of string &allEmp;
To populate the array, we would normally use a looping mechanism of some sort, but to keep this example simple, we will hard-code the data directly:
/* Set up employees */ &singleEmp = CreateArray(""); &singleEmp  = "000045"; &singleEmp  = "Y"; &allEmp = CreateArray(&singleEmp); &singleEmp = CreateArray(""); &singleEmp  = "000089"; &singleEmp  = "Y"; &allEmp.Push(&singleEmp); &singleEmp = CreateArray(""); &singleEmp  = "000132"; &singleEmp  = "N"; &allEmp.Push(&singleEmp); &singleEmp = CreateArray(""); &singleEmp  = "000245"; &singleEmp  = "E"; &allEmp.Push(&singleEmp); &singleEmp = CreateArray(""); &singleEmp  = "000299"; &singleEmp  = "Y"; &allEmp.Push(&singleEmp);
At this point we have a two-dimensional array with five rows and two columns.
C. Accessing the Array
The Len property can be used to determine the number of elements in the array:
/* Check for array length */ &array_len = &allEmp.Len;
With the array populated, you can do a ‘Find’ across the entire array, searching for a key value and then returning the index number of the match. A return value of 0 indicates that no match was found. Note that the ‘Find’ command only works on the first element in the array, the Employee ID in this case:
/* Search for employee */ &pos = &allEmp.Find("000245");
To refer to an array element directly, we must specify two parameters (an x and a y value, if you like). Following on from the previous example, if we wanted to update the status flag for the employee, we could do this:
/* Update status */ If &pos <> 0 Then &allEmp [&pos, 2] = "Y"; End-If;
To see the entire contents of the array, the Join method can be used. This requires one parameter: the character used to perform the join (a comma and space in this example):
/* See the entire array */ &all_string = &allEmp.Join(", "); MessageBox(0, "", 0, 0, &all_string);
The result appears as follows:
See Also:Tip013: Introduction to Arrays