Indexed variables


NetRexx provides an indexed variable mechanism, adapted from the compound variables of Rexx.

NetRexx string variables can be referred to simply by name, or also by their name qualified by another string (the index). When an index is used, a value associated with that index is either set or extracted; in the latter case, the initial value of the variable is returned if the index has not been used to set a value. For example, the program:

  dognoise='bark' 
  dognoise['pup']='yap'
  dognoise['bulldog']='grrrrr'
  say dognoise['pup'] dognoise['terrier'] dognoise['bulldog']

would display

  yap bark grrrrr

Any expression may be used inside the brackets; the resulting string is used as the index. Multiple dimensions may be used, if required:

  dognoise='bark' 
  dognoise['spaniel', 'brown']='ruff'
  say dognoise['spaniel', 'brown'] dognoise['terrier']

which would display

  ruff bark

Here's a more complex example, a test program with a function (called a constant method in NetRexx) that removes all duplicate words from a string of words:

  /* justonetest.nrx -- test the justone function.        */
  say justone('to be or not to be')    /* simple testcase */
  exit

/* This removes duplicate words from a string, and */ /* shows the use of a variable (HADWORD) which is */ /* indexed by arbitrary data (words). */ method justone(wordlist) constant hadword=0 /* show all possible words as new */ outlist='' /* initialize the output list */ loop while wordlist\='' /* loop while we have data */ /* next, split WORDLIST into first word and residue */ parse wordlist word wordlist if hadword[word] then iterate /* loop if had word */ hadword[word]=1 /* remember we have had this word */ outlist=outlist word /* add word to output list */ end return outlist /* finally return the result */

Running this program would display just the four words 'to', 'be', 'or', and 'not'.

This example also uses the built-in string parsing provided by the PARSE statement. In this instance, the value of WORDLIST is parsed, with the first word of the value being assigned to the variable WORD and the remainder being assigned back to WORDLIST (replacing the original value).

[Author's note: since the notation for indexed variables looks just like arrays (see the next section), but does not not suffer the restrictions of arrays, I like to call them disarrays.]


[ previous section | contents | next section ]

From 'nrover.doc', version 1.113.
Copyright(c) IBM Corporation, 1996, 1997. All rights reserved. ©