Using the REXX Language
ID: Q99060
|
SUMMARY
REXX is an OS/2 "batch" style language that is relatively easy to learn
and far more powerful than the MS-DOS batch language. Major features
include user and file input and output, arithmetic, string manipulation,
better program flow control and error handling. This article (and several
related ones in the Knowledge Base) provides a brief introduction to REXX.
MORE INFORMATION
REXX is an OS/2 "batch" style language far more powerful than the MS-DOS
batch language and thus can be used to meet administrative batch file
requirements impossible with MS-DOS. Because many of its features follow
standard math and programming conventions it can be easily learned if you
have any familiarity with programming in general. For those who don't have
any programming background but know the MS-DOS batch language, it is a good
path toward learning more sophisticated languages such as BASIC, PASCAL, C
and Assembler.
DISCLAIMER: This article is provided for users capable of developing
programs with the information presented; it is not an extensive treatment
of the language but rather a quick reference to aid someone in writing
simple routines. Microsoft cannot support programming efforts beyond
reproducing and submitting problems with the language implementation
itself. If you need further assistance, consult REXX references such as
"The REXX Language, A Practical Approach to Programming" by M. F.
Cowlishaw, Prentice-Hall, Englewood Cliffs, 1985.
REXX's major advantages over batch files are its ability to read and write
files, capture user input, do significant string manipulation, handle
arithmetic, control program flow to a much greater degree, and potentially
handle errors more gracefully.
A REXX command file, like a batch file, has a .CMD ending on its file name.
It is distinguished from a MS-DOS command file by a slash-asterisk starting
in row 1 column 1 of the file. This slash-asterisk combination also
signifies the beginning of a REXX comment and therefore must have a
corresponding asterisk-slash ending delimiter. No particular text is
required between the delimiters to signal OS/2 that this is a REXX file.
Unfortunately, the slash-asterisk and asterisk-slash can not appear in
these articles because some programs used to distribute them interpret
these character pairs and modify their output accordingly.
The math symbols + - * / = ( and ) behave exactly as you would
expect them to under standard math conventions. A double * is used to
raise a number to a power. A double forward-slash is used to return the
remainder of a division (example: 7//3 = 1) and the % is used to indicate
the whole number part of division (example: 7%3 = 2). A double &
signifies exclusive-ORing.
The logic symbols < > and = have the expected meanings. A TRUE condition
is signified by 1, FALSE by 0. The \ key represents logical NOT, the
vertical bar ( | ) is logical OR and & is logical AND. The two symbols <>
together represent 'not equal' as does \=.
REXX variables are dynamically assigned and their names must begin
with a letter or ! or ? or the underscore. Numbers are acceptable
as long as they are not the first character. An array can be
represented by a variable name followed by a period such as
'array_var.' and individual elements are represented by what follows
the period such as 'array_var.0'.
In REXX a string is any sequence of characters enclosed in single or
double quotes, if a single or double quote needs to appear in a string
then either use the alternate quote mark to enclose the string (single
quotes around a string containing a double quote and vice versa) or
double up on the quote marks as follows: "This is a way to include
""double quotes"" in a string delimited by double quotes".
A double vertical bar (||) is the string concatenation symbol in REXX.
In most cases concatenation is also performed when variables are separated
by spaces in a list.
More extensive listings of the REXX commands which can be used under OS/2
version 1.3 are available by category in separate articles. Some commonly
used REXX commands are discussed below.
Use EXIT to end any REXX procedure, use RETURN to end a subroutine.
The prime conditional statements in REXX are IF and SELECT. Examples
are given below:
IF condition THEN { THEN is required }
action
ELSE {needed only if an other_action is used}
other_action
SELECT
WHEN condition1 THEN action1
WHEN condition2 THEN action2
.
.
.
WHEN condition_n THEN action_n
OTHERWISE other_action
END
The action can be a DO ... END loop (see below) or other REXX constructs.
For SELECT, only one condition (or the OTHERWISE) is executed. If there is
a possibility that none of the WHENs will be selected, then the OTHERWISE
is required.
If you need to test for a condition where no action is needed (because of
the constraints of the logic you are trying to implement) use the NOP
(NOoPeration) instruction.
The DO command is the looping construct in REXX. DO loops are terminated
with the END statement. There are varying qualifiers which can be applied
as follows:
DO n (loop 'n' number of times)
DO var = startvalue TO endvalue
DO FOREVER
DO WHILE condition
DO UNTIL condition
DO UNTIL does one iteration regardless of the value of condition. To
exit a DO loop use the LEAVE command such as "IF i = 5 THEN LEAVE". To
exit a loop and branch to a specific label use the SIGNAL command (IF
i = 5 THEN SIGNAL label). To bypass the rest of the instructions in a
loop for one cycle use the ITERATE command (IF condition THEN ITERATE).
SAY is virtually equivalent to MS-DOS ECHO except that expression
evaluation is allowed. Example: SAY 4 * 3 would place 12 on the screen.
PULL uppercases everything it reads, use PARSE PULL to maintain case
sensitivity. If multiple arguments are specified, PULL separates each word
in the input up to the number of arguments given and ignores the rest of
the input. If fewer than the number of arguments is supplied on the input,
then the remaining variables are null. If a single argument is given, only
the first word is saved.
Additional query words:
2.10 2.1 2.10a 2.1a 2.20 2.2
Keywords :
Version :
Platform :
Issue type :