Cours NetRexx Language Overview, tutoriel & guide de travaux pratiques en pdf.
Doing things with strings
A character string is the fundamental datatype of NetRexx, and so, as you might expect, NetRexx provides many useful routines for manipulating strings. These are based on the functions of Rexx, but use a syntax that is more like Java or other similar languages:
phrase=’Now is the time for a party’
The second line here can be read from left to right as:
take the variable phrase, find the seventh word, and then find the position of the first “r” in that word.
This would display “3” in this case, because “r” is the third character in “party”.
(In Rexx, the second line above would have been written using nested function calls:
say pos(‘r’, word(phrase, 7))
which is not as easy to read; you have to follow the nesting and then backtrack from right to left to work out exactly what’s going on.)
In the NetRexx syntax, at each point in the sequence of operations some routine is acting on the result of what has gone before. These routines are called methods, to make the distinction from functions (which act in isolation). NetRexx provides (as methods) most of the functions that were evolved for Rexx, including:
• changestr (change all occurrences of a substring to another)
• copies (make multiple copies of a string)
• lastpos (find rightmost occurrence)
• left and right (return leftmost/rightmost character(s))
• pos and wordpos (find the position of string or a word in a string)
• reverse (swap end-to-end)
• space (pad between words with fixed spacing)
• strip (remove leading and/or trailing white space)
• verify (check the contents of a string for selected characters)
• word, wordindex, wordlength, and words (work with words).
These and the others like them, and the parsing described in the next section, make it especially easy to process text with NetRexx.
The previous section described some of the string-handling facilities available; NetRexx also provides string parsing, which is an easy way of breaking up strings of characters using simple pattern matching.
A parse instruction first specifies the string to be parsed. This can be any term, but is often taken simply from a variable. The term is followed by a template which describes how the string is to be split up, and where the pieces are to be put.
Parsing into words
The simplest form of parsing template consists of a list of variable names. The string being parsed is split up into words (sequences of characters separated by blanks), and each word from the string is assigned (copied) to the next variable in turn, from left to right. The final variable is treated specially in that it will be assigned a copy of whatever is left of the original string and may therefore contain several words. For example, in:
parse ‘This is a sentence.’ v1 v2 v3
the variable v1 would be assigned the value “This”, v2 would be assigned the value “is”, and v3 would be assigned the value “a sentence.”.
A literal string may be used in a template as a pattern to split up the string. For example
parse ‘To be, or not to be?’ w1 ‘,’ w2 w3 w4
would cause the string to be scanned for the comma, and then split at that point; each section is then treated in just the same way as the whole string was in the previous example.
Thus, w1 would be set to “To be”, w2 and w3 would be assigned the values “or” and “not”, and w4 would be assigned the remainder: “to be?”. Note that the pattern itself is not assigned to any variable.
The pattern may be specified as a variable, by putting the variable name in parentheses.
The following instructions:
parse ‘To be, or not to be?’ w1 (comma) w2 w3 w4
therefore have the same effect as the previous example.
The third kind of parsing mechanism is the numeric positional pattern. This allows strings to be parsed using column positions.
Version 2.01 Copyright (c) IBM Corporation 1996, 2003. All rights reserved.
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 qual-ified by another string (the index). When an index is used, a value associated with that index is either set:
fred=0 –– initial value
fred=’abc’ –– indexed value
say fred –– 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:
say bark[‘pup’] bark[‘terrier’] bark[‘bulldog’]
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:
say bark[‘spaniel’, ‘brown’] bark[‘terrier’] bark[‘bull’animal]
which would display
ruff woof grrrrr
Version 2.01 Copyright (c) IBM Corporation 1996, 2003. All rights reserved. 9
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 */
return outlist /* finally return the result */
Running this program would display just the four words “to”, “be”, “or”, and “not”.
Expressions and variables
Doing things with strings
Things that aren’t strings
Binary types and conversions
Exception and error handling
Summary and Information Sources