NetRexx Language Overview

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’
say phrase.word(7).pos(‘r’)
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.

Parsing strings

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.”.

Literal patterns
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:
comma=’,’
parse ‘To be, or not to be?’ w1 (comma) w2 w3 w4
therefore have the same effect as the previous example.

Positional patterns
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.

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 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[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
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 */
end
return outlist /* finally return the result */
Running this program would display just the four words “to”, “be”, “or”, and “not”.

NetRexx Overview 
NetRexx programs
Expressions and variables
Control instructions
NetRexx arithmetic 
Doing things with strings
Parsing strings
Indexed strings
Arrays
Things that aren’t strings
Extending classes
Tracing
Binary types and conversions 
Exception and error handling
Summary and Information Sources

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *