| REXX Workshop - Part 3 |
line1 = 'The merry wives of Windsor'
Parse Var line1 v1 v2 v3 v4 v5
/* v1 receives "The" */
/* v2 receives "merry" */
/* v3 receives "wives" */
/* v4 receives "of" */
/* v5 receives "Windsor" */
line1 = 'The merry wives of Windsor'
Parse Var line1 v1 v2 v3 v4
/* v1 receives "The" */
/* v2 receives "merry" */
/* v3 receives "wives" */
/* v4 receives "of Windsor" */
line1 = 'The merry wives of Windsor'
Parse Var line1 v1 v2 v3 v4 .
/* v1 receives "The" */
/* v2 receives "merry" */
/* v3 receives "wives" */
/* v4 receives "of" */
/* "Windsor" is discarded */
line1 = 'The merry wives of Windsor'
Parse Var line1 . v2 . v4 .
/* "The" is discarded */
/* v2 receives "merry" */
/* "wives" is discarded */
/* v4 receives "of" */
/* "Windsor" is discarded */
But it may be of interest to remove one word from a string. Examine the following code snippet.
line1 = 'The merry wives of Windsor'
Parse Var line1 v1 line1
/* v1 receives "The" */
/* line1 receives "merry wives of Windsor" */
It is also possible to parse strings by absolute column number. Observe the following snippet; arguments on the PARSE instruction include absolute column numbers which delimit the substrings assigned to the variables.
line1 = 'abcdefghijklmnopqrstuvwxyz'
Parse Var line1 1 v1 4 . 20 v2 22 . 26 v3
/* v1 receives "abc" */
/* v2 receives "tu" */
/* v3 receives "z"" */
line1 = '11:25:01'
Parse Var line1 hh ':' mm ':' ss
/* hh receives "11" */
/* mm receives "25" */
/* ss receives "01" */
TOTAL 15 22 17
/* TOTAL EXEC */
Parse Arg nmen nwomen nchildren .
/* nmen receives "15" */
/* nwomen receives "22" */
/* nchildren receives "17" */
The number of words supplied to an exec as the argument can be determined by examining the results of "ARG(1)" as in the following example.
/* TOTAL EXEC */
n = Words(Arg(1))
/* n receives the number of words */
/* in the argument string. */
sum = TOTALUP("15 22 17")
sum = TOTALUP(15, 22, 17)
sum = TOTALUP("15 22 17")
.
.
.
TOTALUP:
Parse Arg nmen nwomen nchildren .
.
.
.
sum = TOTALUP(15, 22, 17)
.
.
.
TOTALUP:
nmen = Arg(1)
nwomen = Arg(2)
nchildren = Arg(3)
.
.
.
x = 3
or
s = 'Dumbo'
name.1
name.2
name.3
.
.
.
name.n
All the elements of a stem variable can be referred to by the stem name:
name.
v = animal.1 /* v receives 'ANIMAL.1' */
Stem variables can be assigned values like any scalar variable,
animal.1 = 'monkey'
animal.2 = 'turkey'
animal.3 = 'mouse'
animals = animal.1 animal.2 animal.3
/* animals receives "monkey turkey mouse" */
animal. = 'ant' /* all elements set to "ant" */
or
animal. = '' /* all elements set to null */
animal.1 = 'monkey'
animal.2 = 'turkey'
animal.3 = 'mouse'
then animal.0 should hold the value of "3". Please note that the
zeroth element is not automatically set. Some commands set the value
correctly; at other times, the program must explicitly set the value.
animal.0 = 3
animal.1 = 'monkey'
animal.2 = 'turkey'
animal.3 = 'mouse'
Do i = 1 To animal.0 /* i takes on the */
Say animal.i /* values 1, 2, 3 */
End
pet.1.1 = 'cat'
pet.1.2 = 'tiger'
pet.1.3 = 'lion'
pet.2.1 = 'dog'
pet.2.2 = 'wolf'
pet.2.3 = 'coyote'
'EXECIO * DISKR TEST DATA A (STEM LINES. FINIS'
where
Assuming that "TEST DATA A" contains three records, the following
exec reads all three records and then displays them on the screen.
Please note that EXECIO automatically initializes the zeroth
element of the stem variable to be equal to the number of records
retrieved from the file.
/* Example of EXECIO Read Operation */
'EXECIO * DISKR TEST DATA A (STEM LINES. FINIS'
Do i = 1 To lines.0
Say lines.i
End
Exit
In the following example, three elements of the stem variable are initialized. Please note that the number of lines ("3") is indicated as the first argument after the EXECIO command. "DISKW" indicates that a "disk write" operation is required. In this example, the file "TEST DATA A" will be created if it does not exist. If the file exists already, the lines will be appended to the existing file.
/* Example of EXECIO Write Operation */
lines.1 = 'Chickens'
lines.2 = 'Cows'
lines.3 = 'Crows'
'EXECIO 3 DISKW TEST DATA A (STEM LINES. FINIS'
Exit
HELP EXECIO
or other documentation.
Programming with Pipelines is extremely economical because of the amount of work performed by various Pipeline stages. But a thorough discussion of Pipelines is beyond the scope of this effort. A few brief examples should provide sufficient knowledge to use Pipelines to read disk files into a REXX exec and to write information from a REXX exec out to a disk file.
The following pipeline reads a CMS disk file named "TEST DATA A" into a stem variable within a REXX exec.
'PIPE < TEST DATA A | STEM LINES.'
where
Assuming that "TEST DATA A" contains three records, the following
exec reads all three records and then displays them on the screen.
Please note that the STEM stage automatically initializes the zeroth
element of the stem variable to be equal to the number of records
retrieved from the pipe.
/* Example of Input with Pipelines */
'PIPE < TEST DATA A | STEM LINES.'
Do i = 1 To lines.0
Say lines.i
End
Exit
Conversely, data from a REXX exec can be written out to a disk file. Two variations are considered.
In the following example, the contents of a stem variable are written out to a disk file. If the disk file does not exist, it is created. If the disk file exists, its contents are replaced. Please note that the zeroth element of the stem variable must be initialized to a positive whole number equal to the number of variables which are to be written to the disk file.
/* Example of Output with Pipelines */
lines.1 = 'Chickens'
lines.2 = 'Cows'
lines.3 = 'Crows'
lines.0 = 3
'PIPE STEM LINES. | > TEST DATA A'
Exit
where
The values of REXX scalar values can also be written out to a file with Pipelines using the VAR stage. For example,
/* Example of Output with Pipelines */
line1 = 'Chickens'
line2 = 'Cows'
line3 = 'Crows'
'PIPE VAR LINE1 | > TEST DATA A' /* Creates the file */
'PIPE VAR LINE2 | >> TEST DATA A' /* Appends to the file */
'PIPE VAR LINE3 | >> TEST DATA A' /* Appends to the file */
Exit
where
HELP PIPE MENU
In the following example, characters are read one-by-one from a file and displayed on the screen.
/* Example of Reading Characters with CHARIN */
fileid = 'TEST DATA A' /* name of stream */
Do i = 1 By 1
If CHARS(fileid)=0 /* more characters? */
Then Leave
Say CHARIN(fileid,,1) /* display one character */
End
f = STREAM(fileid,'COMMAND','CLOSE') /* close stream */
Exit
where
/* Example of Writing a Data with CHAROUT */
fileid = 'TEST DATA A' /* name stream */
Call CHAROUT fileid,'One...' /* write */
Call CHAROUT fileid,'Two...' /* write */
Call CHAROUT fileid,'Three...' /* write */
f = STREAM(fileid,'COMMAND','CLOSE') /* closes stream */
Exit
where
One...Two...Three...
HELP REXX CHARS
HELP REXX CHARIN
HELP REXX CHAROUT
HELP REXX STREAM
In the following example, lines of data are read from a CMS file named "TEST DATA A" and displayed on the screen. At the end of the loop, the STREAM( ) built-in function is used to close the stream/file so that it can be reused.
/* Example of Reading a File with LINEIN( ) */
fileid = 'TEST DATA A' /* name of "stream" */
nlines = LINES(fileid) /* number lines in stream */
Do i = 1 To nlines
Say LINEIN(fileid) /* displays a line */
End
f = STREAM(fileid,'COMMAND','CLOSE') /* closes stream */
Exit
where
/* Example of Writing a File with LINEOUT */
fileid = 'TEST DATA A' /* name of "stream" */
Call LINEOUT fileid,'This is line one.' /* write */
Call LINEOUT fileid,'This is line two.' /* write */
Call LINEOUT fileid,'This is line three.' /* write */
f = STREAM(fileid,'COMMAND','CLOSE') /* closes stream */
Exit
where
HELP REXX LINES
HELP REXX LINEIN
HELP REXX LINEOUT
HELP REXX STREAM