NetRexx Overview, version 2.02
Copyright (c) IBM Corporation, 2001. All rights reserved. ©
22 May 2001
[previous | contents | next]

Indexed strings

NetRexx provides indexed strings, adapted from the compound variables of Rexx. Indexed strings form a powerful ‘associative lookup’, or dictionary, mechanism which can be used with a convenient and simple syntax.

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:


  fred=0         -- initial value

  fred[3]='abc'  -- indexed value

or retrieved:

  say fred[3]    -- would say "abc"

in the latter case, the simple (initial) value of the variable is returned if the index has not been used to set a value. For example, the program:

  bark='woof'

  bark['pup']='yap'

  bark['bulldog']='grrrrr'

  say bark['pup'] bark['terrier'] bark['bulldog']

would display

  yap woof grrrrr

Note that it is not necessary to use a number as the index; any expression may be used inside the brackets; the resulting string is used as the index. Multiple dimensions may be used, if required:

  bark='woof'

  bark['spaniel', 'brown']='ruff'

  bark['bulldog']='grrrrr'

  animal='dog'

  say bark['spaniel', 'brown'] bark['terrier'] bark['bull'animal]

which would display

  ruff woof grrrrr

Here's a more complex example using indexed strings, a test program with a function (called a static 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) static

    hadword=0         /* show all possible words as new */

    outlist=''            /* initialize the output list */

    loop while wordlist\=''  /* loop while we have data */

      /* 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’.
[previous | contents | next]

From The NetRexx Language by Mike Cowlishaw, mfc@uk.ibm.com (ISBN 0-13-806332-X, 197pp, Prentice-Hall, 1997).