Using Compound Variables and Stems
employee could contain names as follows:
EMPLOYEE
(1) Adams, Joe
(2) Crandall, Amy
(3) Devon, David
(4) Garrison, Donna
(5) Leone, Mary
(6) Sebastian, Isaac
In some computer languages, you use the number of the element to access an element in an array. For example, employee(1) would retrieve Adams, Joe. In REXX, you use compound variables.
What is a Compound Variable?
You can use compound variables to create an array or a list of variables in REXX. A
compound variable, for example: employee.1, consists of a stem and a
tail.
FRED.
Array.
employee. A tail is similar to a subscript. It follows the stem and
consists of additional parts of the name that can be constant symbols (as in
employee.1 ), simple symbols (as in employee.n ), or null. Thus,
in REXX, subscripts need not necessarily be numeric. A compound variable contains at least one
period with characters on both sides of it. Here are some more examples of compound variables:
FRED.5
Array.Row.Col
employee.name.phone
employee.7='Amy Martin'
new=7
employee.new='May Davis'
say employee.7 /* Produces: May Davis */As with other REXX variables, if you
have not previously assigned a value to a variable in a tail, it takes on the value of its own name
in uppercase. first = 'Fred'
last = 'Higgins'
name = first.last /* NAME is assigned FIRST.Higgins */
/* The value FIRST appears because the */
/* variable FIRST is a stem, which */
/* cannot change. */
SAY name.first.middle.last /* Produces NAME.Fred.MIDDLE.Higgins */
DO i = 1 TO 6
PARSE PULL employee.i
END
employee.1 = 'Adams, Joe'
employee.2 = 'Crandall, Amy'
employee.3 = 'Devon, David'
employee.4 = 'Garrison, Donna'
employee.5 = 'Leone, Mary'
employee.6 = 'Sebastian, Isaac'After the names are in the group of compound variables,
you can easily access a name by its number or by a variable that represents its number.
name = 3
SAY employee.name /* Produces 'Devon, David' */
For more information about compound variables, see Compound symbols.
Using stems
When working with compound variables, it is often useful to initialize an entire collection of
variables to the same value. You can do this easily by using an assignment that includes a stem. For
example, number.=0 initializes all array elements in the array named
number. to 0.
Nobody , use the following assignment instruction:
employee. = 'Nobody'
employee.,
previously assigned or not, have the value Nobody. After a stem assignment, you can
assign individual compound variables new values. employee.='Nobody'
SAY employee.5 /* Produces 'Nobody' */
SAY employee.10 /* Produces 'Nobody' */
SAY employee.oldest /* Produces 'Nobody' */
employee.new = 'Clark, Evans'
SAY employee.new /* Produces 'Clark, Evans' */
You can use stems with the EXECIO and RFS commands when reading to and writing from a file. See EXECIO and RFS. RFS is the preferred I/O method under CICS®.
Exercises - Using Compound Variables and Stems
- After these assignment instructions, what do the following SAY instructions produce?
a = 3 /* assigns '3' to variable 'A' */ d = 4 /* '4' to 'D' */ c = 'last' /* 'last' to 'C' */ a.d = 2 /* '2' to 'A.4' */ a.c = 5 /* '5' to 'A.last' */ z.a.d = 'cv3d' /* 'cv3d' to 'Z.3.4' */- SAY a
- SAY D
- SAY c
- SAY a.a
- SAY A.D
- SAY d.c
- SAY c.a
- SAY a.first
- SAY z.a.4
- After these assignment instructions, what output do the SAY instructions produce?
hole.1 = 'full' hole. = 'empty' hole.s = 'full'- SAY hole.1
- SAY hole.s
- SAY hole.mouse
-
- 3
- 4
- last
- A.3
- 2
- D.last
- C.3
- A.FIRST
- cv3d
-
- empty
- full
- empty