Channels ▼
RSS

Open Source

The Power of Associative Arrays


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 :

 
aa = strip(list_a.a) /* Strip out any preceding/trailing blanks   */

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:

 
flag.aa = 1  /*  Mark the name with a 1 */

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:

 
do b = 1 to list_b.0 /* Look for matching name from 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:

 
bb = strip(list_b.b) /*  Put LIST_B name into 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  \ flag.bb then do  /* If the name doesn't exist in FLAG array */

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:

 
m = m+1         /*    add 1 to the count of missing names */
missing.m = bb  /*    add missing name to MISSING array   */

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:

 
missing.0 = m  /* Save the count of unmatched names     */

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video