In Rexx, arrays are expressed in the form of compound variables, two or more variable names strung together by periods. The entire array is referenced by the name of the array followed by a period.
The first line of code in Listing One refers to an array named flag, denoted by the array name flag followed immediately by a period. So this first line creates the associative array named flag and initializes all possible elements to 0:
flag. = 0 /* Create an array, initialize elements to 0 */
All Rexx arrays are dynamic, so we do not need to specify a size for the flag array.
In Rexx, you commonly store the number of array elements in the first array position (denoted by the 0 subscript). So for an array named list_a, array element list_a.0 holds the number of items in the array. This do loop thus processes all the names in the array named list_a:
do a = 1 to list_a.0 /* Process all the names in LIST_A array */
The first line inside the do loop removes any leading or trailing blanks from the name through the strip function. It places the result in the variable
Next we mark the presence of the name in the flag array by flagging it. Here you see the use of the associative array. We denote that a value exists simply by using that value as the subscript into the flag array:
At the conclusion of the do loop, the flag array consists of a group of name indexes that are flagged as present.
The second do loop looks at each name in the second array, called list_b, and sees if it exists as a flagged member in the flag array. If so, we have matched names between the two lists, list_a and list_b.
The first line in the second do loop processes all names in the second array, called list_b:
The next line in the second do loop removes leading and trailing blanks from a name in list_b, and places that name into the variable bb:
Now we can subscript the flag array with this name from the second list. If it does not exist in the flag array (denoted by the backslash symbol "\" meaning "NOT"), then we know we have a name from the second list that does not exist in the first list:
If the name does not exist, we add 1 to the count of unmatched names. We also add the missing name to the list of missing names in the array we've named missing:
There is no need to "declare" or pre-define an array in Rexx. Define it simply by using it, as we do above in our first reference to the missing array.
The last line in the routine sets the total count of missing names in the missing array. In Rexx, by convention we store this value as element 0 in that array:
After the code executes, the missing array contains all names from the second list that are not in the first list. The first element of the missing array, missing.0, contains the number of unmatched names.
aa = strip(list_a.a) /* Strip out any preceding/trailing blanks */
flag.aa = 1 /* Mark the name with a 1 */
do b = 1 to list_b.0 /* Look for matching name from LIST_B */
bb = strip(list_b.b) /* Put LIST_B name into variable BB */
if \ flag.bb then do /* If the name doesn't exist in FLAG array */
m = m+1 /* add 1 to the count of missing names */
missing.m = bb /* add missing name to MISSING array */
missing.0 = m /* Save the count of unmatched names */