PeopleCode | Using arrays to make your code more efficient
A. Benefits of Arrays
Although arrays are a common feature in most programming languages, they are not so regularly used in PeopleCode. This is a shame as arrays can potentially offer a lot in terms of code readability and performance.
In particular, arrays are useful if you’re dealing with a situation that involves repeated access to a small set of data values created on an ad-hoc basis, such as months of the year, valid employee department codes, student career types, or payroll deduction codes. Typically, this set of values is constantly referenced for the purposes of data validations and calculations. Rather than repeatedly running SQL select statements from the database – or continually looping through a rowset object in order to find matches – set up your predefined list of values in an array and then access the array alone for further processing.
To keep this post at an introductory level, we will only consider one-dimensional arrays. While it is possible to create multi-dimensional arrays in PeopleCode – in fact, you can go right up to 15 dimensions – we will stick to the simplest array type for now.
B. Declaring Arrays
Firstly, here is an example of declaring an array of strings:
Local array of string &gradeCodes = CreateArray("");
Unlike other programming languages, there is no requirement in PeopleCode to define the number of array elements in advance. The above statement defines an empty array without specifying an initial length. If you want to immediately define the array with a certain number of items, use the ‘CreateArrayRept’ function:
Local array of string &flagsArray = CreateArrayRept("Y", 10);
This creates an array with ten items, each populated with ‘Y’.
The first parameter passed into ‘CreateArray’ or ‘CreateArrayRept’ must match the type of array being created. So in other words, a string array must be initialised with a string value, even if it’s just the empty string character, as in the ‘CreateArray’ example above.
C. Populating Arrays
To populate the array, refer to each element of the array via the use of an index value in square brackets. Note that the array index starts at ‘1’. Again, unlike other programming languages, there is no such thing as an array index of ‘0’ in PeopleCode.
For &i = 1 to &dataRS.ActiveRowCount &dataRec = &dataRS.GetRow(&i).GetRecord(Record.RECORD_NAME); &gradeCodes [&i] = &dataRec.CRSE_GRADE_INPUT.Value; end-for;
Alternatively, you can directly hard-code the values of the array when you first initialise it via the use of ‘CreateArray’ (hard-coding of course is not recommended in general):
&exemptionTypes = CreateArray("1", "2", "5", "6", "7", "8", "D");
As another alternative again, you can use the ‘Push’ and ‘Unshift’ methods to add new items to the array. These two methods work in a similar way, except that ‘Push’ adds a new item at the end of the array (a ‘Push’ on a three item array will create a new item [4]), while ‘Unshift’ adds a new item at the start of the array (an ‘Unshift’ on a three-item array will create a new item [1], while shifting the existing three elements to [2], [3], and [4]).
&gradeCodes.Push("1"); /* array item 1 */ &gradeCodes.Push("2"); /* array item 2 */ &gradeCodes.Push("5"); /* array item 3 */
If you ever need to remove items from an array, ‘Pop’ and ‘Shift’ work in the opposite way to the ‘Push’ and ‘Unshift’ commands. Personally speaking, I have rarely (if ever) had to remove items from an array, so am only mentioning these two methods in passing.
D. Searching Arrays
If you want to search the array for a particular value – a much more common programming requirement – the ‘Find’ method comes in useful. In fact, this is probably the best thing about arrays in general: the simplicity and speed of performing an array search:
/* Check to see the current employee has an approved exemption type */ If &exemptionTypes.Find(&employeeExemptionType) > 0 then /* A match has been found – continue processing */ else /* No match found */ end-if;
In the above example, if the ‘Find’ method locates a match, it will return the index number of the match. So using the Exemption Types array as an example, if we searched for exemption type ‘5’, ‘Find’ would return a value of 3:
&exemptionTypes = CreateArray("1", "2", "5", "6", "7", "8", "D");
If the same value happened to exist more than once in the array, ‘Find’ would return the index of the first match only. A return value of ‘0’ indicates that no match was found.
Finally, the ‘Len’ property returns the number of elements currently stored in the array:
&arrayLength = &gradeCodes.Len;
See Also:
Tip 040: Setting Column Labels in Grids
Tip 046: File Manipulation in PeopleCode
Tip 076: Two-Dimensional Arrays