Professional Documents
Culture Documents
What is REXX ?
Restructured EXtended eXecutor
language
Programming language
| Inclusive OR
(4 > 2) | (5 = 3) /* at least one is true, so result is 1 *
(2 > 4) | (5 = 3) /* neither one is true, so result is 0 */
&& Exclusive OR
(4 > 2) && (5 = 3) /* only one is true, so result is 1 */
(4 > 2) && (5 = 5) /* both are true, so result is 0 */
/*REXX*/
name=‘Roosevelt’
say ‘Welcome to TSO ‘ name
exit
• Output
Welcome to TSO Roosevelt
REXX instruction - PULL
• To read the input from the TSO terminal (when REXX
data stack is empty)
• Syntax : pull var1 var2 var3…
/*REXX*/
say ‘Enter your name :’
pull name
say ‘Good morning ‘ name
exit
• The output will be
Enter your name :
Lincoln
Good morning LINCOLN
• pull automatically converts the input into uppercase
REXX Instruction - IF
• Used to conditionally execute a single stmt or a group of
statements
• Syntax : if expression then
stmt;
else
stmt;
• A group of statements can be grouped together by using do…
end
• Nested IF’s are allowed
REXX Instructions - NOP
• nop stands for No-operation
• Causes REXX to create No-operation condition
• Only useful as a target of THEN or ELSE clause
REXX Instructions - IF
/*REXX*/
pull ampm
else
if ampm = 'PM' then
say 'Good evening!!!'
else NOP
exit
REXX Instructions - DO
• Used to execute a group of statements under the control
of an expression
• Has several formats
• The repetitive DO construct
do 5 /* do x=5 is the same */
say ‘hi there!!!’
end
• The output
1
2
Contd...
REXX Instructions - DO
• The do..until construct
/*REXX*/
i=1
do until i > 3
say i
i= i+ 1
end
• The output will be
1
2
3
Contd...
REXX Instructions - DO
• The do..forever special construct
/*REXX*/
do forever
say ‘infinite loop’
end
• The above exec results into an infinite loop
• Enough care should be taken to check the exit criteria of
the loop, before executing the exec
• The leave instruction can be used to exit from the loop
REXX Instructions - LEAVE
• Causes REXX to stop executing the current do…end loop and
control passes to the next statement after the end of the do…end pair
/*REXX*/
do forever
say ‘Enter the code :’
pull code
if code = ‘BYE’ then
do
leave;
end
End
say ‘Statement after do…end’
exit
REXX Instructions - ITERATE
• Used to restart execution in a do loop
ITERATE {name}
• If name is not specified, ITERATE will step the
innermost active loop
• If name is specified, that particular loop is stepped
/*REXX*/
do i = 1 to 3 by 1
if i = 2 then
iterate i
say ‘i = ‘ i
end
• Output
i=1
i=3
REXX Instructions - SELECT
• Causes REXX to execute one of several different
instructions
• Most used when one of the several paths must be followed
/*REXX*/
say ‘Enter 1 for salad,2 for pizza :’
pull choice
select
when choice = 1 then
do
say ‘Here is the salad’
end
when choice = 2 then say ‘Here is the pizza’
otherwise say ‘You have opted nothing’
end
REXX Instruction - UPPER
• To translate the contents of a single or several variables
to
uppercase
• Syntax : upper var1 var2 var3 …
/*REXX*/
name =‘Kennedy’
say ‘Name is ‘ name
upper name
say ‘Now name is ‘ name
exit
• Output
Name is Kennedy
Now name is KENNEDY
REXX Instruction - EXIT
drop d.
REXX Instructions - ADDRESS
• To indicate the destination of non-rexx commands
• Syntax ADDRESS destination
• The destination could be any one of these
TSO - Routes commands to TSO
ISPEXEC - Routes commands to ISPF/PDF
ISREDIT - Routes commands to the ISPF Edit macro
processor
Address tso ‘lista st’
Address tso ‘listds(‘hclt.rexx.exec’) members’
Address ispexec /* Dest. Changed and set */
REXX Instructions - Procedure
• Used to protect the variables in existence when the function
or subroutine is called
• Syntax : PROCEDURE { EXPOSE name1 {name2}…}
name1, name2 are not protected from the subroutine.
That is they are exposed to the subroutine
REXX Instructions - Procedure
Example
/*REXX */
lines=2
pages=3
call showlines
say 'The line count is' lines ',the page count is' pages
exit
read_routine:
address TSO “alloc da(‘”dsnanme”’) F(indd) shr reuse”
if rc = 0 then
do
address TSO “execio * diskr indd (finis stem rec_stm.”
end
else
do
say ‘Error in allocating the file ‘dsnanme
end
TSO Samples - Write Routine
write_routine:
address TSO “alloc da(‘”dsnanme”’) F(indd) shr reuse”
if rc = 0 then
do
address TSO “execio * diskw indd (finis stem rec_stm.”
end
else
do
say ‘Error in allocating the file ‘dsnanme
end
TSO Samples – Validate Dataset
Validate_routine:
if sysdsn(“’”dsnname”’”) = ‘ok’ then
do
say “ present”
end
TSO Samples – File Creation
Create_routine:
address tso “alloc da(‘”psfile”’) new “,
“ Lrecl(80) recfm(f b) “,
“ dsorg(ps) space(1,1) tracks”
TSO Samples – File Deletion
Delete_routine:
if sysdsn(“’”psfile”’”) = ‘ok’ then
do
“delete ‘”psfile”’ “
end
TSO Sample – Read PDS members
Read_pds_routine:
trap_var = outtrap(pds_stm.)
Address TSO “listds ‘”pds_name”’ mem”
if rc /= 0 then
do
say ‘listds error’
end
trap_var = outtrap(off)
do index1 = 7 to pds_stm.0
member_name = strip(pds_stm.index1)
end
TSO Sample – Output
• Built-in functions
Example
/*REXX*/
say address() /* returns TSO */
address ispexec
say address() /* returns ISPEXEC */
address isredit /* returns ISREDIT */
say address()
exit
REXX Functions - CENTER
• Used to center one string within a certain length area and
pad on the left and right of the centered string with an
optional padding character
• Syntax : CENTER(string, length{,pad})
• The default padding character is spaces
Examples :
say center(‘uswest’,10,’*’)
Output:
**uswest**
REXX Functions - COMPARE
• Used to compare two strings and return a zero if the
strings are the same ,or a non-zero number if they are not
• Non-zero number is the position of the first mismatching
character found
• Syntax : COMPARE(string1,string2{,pad})
• If the optional padding character is specified,then the
shorter string is padded and compared
• Examples :
COMPARE('123','123') returns a 0 (exact match)
COMPARE('FO?? ','FO','?') returns a 5 (1st mismatch after padding)
COMPARE('abc','ak') returns a 2 (first mismatching char)
COMPARE('ZZ ','ZZ','x') returns a 3 (1st mismatch found 3 chars
in)
COMPARE('xy ','xy',' ') returns a 0 (exact match with padding)
REXX Functions - COPIES
number of times
• Syntax : COPIES(string,n)
• Examples :
COPIES('Hello',4) returns 'HelloHelloHelloHello'
COPIES('Say what?',0) returns '' (null string)
REXX Functions - DATATYPE
• Used to determine the data type of the string passed
• Syntax : DATATYPE(string{,type})
• If type is omitted,NUM is returned if the string is a valid
number and CHAR is returned in all other cases
• If type is specified,either TRUE(1) or FALSE(0) is returned
• The valid types are as follows
A - Alphanumeric
N - Numeric
W - Whole number
L - Lowercase
U - Uppercase
M - Mixed case
• Examples
DATATYPE(' 44 ') returns NUM (numeric)
DATATYPE('*1**') returns CHAR (caharcter string)
DATATYPE('Wally','M') returns a 1 (mixed case)
DATATYPE('75.54','W') returns a 0 (not a whole number)
REXX Functions - DATE
• Returns the current date
• An optional character can be passed to obtain date in
specific formats
• Syntax : DATE({option})
• Some of the chars that can be passed are
U returns date in USA format, 'mm/dd/yy’
J returns a Julian date in the form 'yyddd’
W returns the day of the week (e.g. 'Tuesday', 'Sunday', etc.)
• Examples
say date() /* returns 17 Dec 1999 */
say date('U') /* returns 12/17/99 */
say date('J') /* returns 99351 */
say date('W') /* returns Friday */
REXX Functions - DELSTR
• Used to delete or remove one string from within another
string
• Syntax : DELSTR(string,n{,length})
• 'string' is the string from which a portion is to be deleted
starting with character number 'n', where 'n' is a positive
integer
• The length of the portion to be deleted is given by the
optional length parameter
• When ‘n’ is greater than the length of the string no action is
performed
• Examples
DELSTR('abcde',3,2) deletes 'cd', leaving 'abe'
DELSTR('zyxw',3) leaves 'zy', deleting 'xw'
DELSTR('12345',6) no change, since 6 is greater than string length
REXX Functions - INDEX
• Used to find the position of one character string within
another character string
• Syntax : INDEX(string,substring{,start})
• 'start' is an optional starting character position for the
search within ‘string’
• Examples
INDEX('hello','ll') returns a 3
INDEX('say what','w ') returns a 5
INDEX('zyxwvu','vu',6) returns a 0
INDEX('zyxwvu','vu',2) returns a 5
REXX Functions - LEFT
• Used to extract the leftmost characters of the string
• Syntax : LENGTH(string,length{,pad})
• If string is shorter than length,the string returned is
padded with ‘pad’ char in the function call if available or
with the default pad character blank.
• Examples :
LEFT('Wallawalla',4) returns 'Wall'
LEFT('Republicans',20,'-') returns 'Republicans---------'
LEFT('Motley Crue ',8) returns 'Motley C'
REXX Functions - LENGTH
/*REXX*/
x=outtrap("a.") /* turns trap on. x=a. */
"listds 'rhkrish.rexx.exec' members"
x=outtrap("off") /* turns trap off. x=off */
say 'No of lines trapped ' a.0
EXIT
OUTTRAP - External function
Contd..
• The output of the exec could be this
No of lines trapped 57
• Note that the no of lines trapped is stored in A.0
• All the trapped lines from A.1 to A.n can be used
• The outtrap function can be used to trap only a certain no
of lines.
OUTTRAP(“A.”,10)
Only 10 lines trapped.
SYSDSN - External Function
• Returns OK if the specified dataset exists; Otherwise
returns appropriate error messages
• Example call
available = SYSDSN(“’HCLT.rexx.exec’”)
/* available could be set to "OK" */
Address TSO
"ALLOC F(IN) DA('SYS1.PROCBLIB(ASMHCL)') SHR"
"ALLOC F(OUT) DA(‘TATA001.DELETE.ME’) SHR"
"FREE F(IN,OUT)"
SAY ‘No of lines in input : ‘ data.0
EXIT
Note : This example uses stem variable data to read the contents of input
file.The Number of lines read will be stored in data.0.
REXX Command -
NEWSTACK
• used to create a new data stack in the REXX environment
• When this command is executed, the current data stack is
saved and a new stack is created and made the current stack
• When the stack is empty,the subsequent pull instruction
obtains input from the TSO terminal
• When the stack has data elements,the pull instruction gets
the input from the top of the stack
• Example
"NEWSTACK" /* creates new stack */
Push tcs /* puts ‘tcs’ in top of stack */
Push uswest /* puts ‘uswest’ over ‘tcs’ */
pull data /* pulls data from the top of stack */
say ‘from the stack ‘ data /* displays ‘uswest’ */
pull data /* pulls data from top of stack */
say ‘from the stack ‘ data /* displays ‘tcs’ */
pull data /* obtains input from tso terminal */
REXX Command - DELSTACK
• used to delete the data stack that was created last in the
REXX environment
• When this command is executed the most recently created
stack is deleted and all elements on it are purged
• If there is any previous data stack,that is made available
• Example :
“NEWSTACK” /* new stack is created */
push a /* ‘a’ is stored on top */
queue b /* ‘b’ is stored at the bottom */
“NEWSTACK” /* new stack is created */
push c /* ‘c’ is stored on top */
say queued() /* displays 1 */
“DELSTACK” /* deletes the current stack */
say queued() /* displays 2 */
“DELSTACK” /* deletes the stack */
FILE TAILORING
File Tailoring - Skeletons
• Skeletons are members of a PDS that have variables and
fixed text.
• The skeleton files can contain variable-length records,
with a maximum record length of 255.
address ispexec,
"LIBDEF ISPSLIB DATASET
ID('USAKRB.DMT.PHASE2.ISPSLIB') "
File Tailoring - FTOPEN
• Example
Address ISPEXEC
“LMINIT DATAID(DDVAR) DATASET(‘HCL.REXX.TEXT')
ENQ(SHR)”
LMMOVE – ISPF Service
• Example
Address ISPEXEC
“LMMOVE FROMID(from-data-id)
[FROMMEM(from-member-name)]
TODATAID(to-data-id)
[TOMEM(to-member-name)]
[REPLACE]“
LMCOPY – ISPF Service
• Example
Address ISPEXEC
“LMCOPY FROMID(from-data-id)
[FROMMEM(from-member-name)]
TODATAID(to-data-id)
[TOMEM(to-member-name)]
[REPLACE]“
LMCMP – ISPF Service
• Example
Address ISPEXEC
“LMCOMP DATAID(data-id)“