![]()
|
Indexed stringsNetRexx 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 valueor 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 grrrrrNote 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 grrrrrHere'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,
|