You are on page 1of 160

2/16/2011 1

REXX
Objectives
› Introduction to REXX

› Syntax and Functions

› Advanced Concepts

2/16/2011 2
Introduction

› What is REXX ?
± Restructured EXtended eXecutor
± Interpreted command language
± Very useful for linking TSO, ISPF and other
functions
± Useful for developing custom-made utilities

2/16/2011 3
Features of REXX
› Ease of use

› Free format

› Convenient built-in functions

2/16/2011 4
Features of REXX (Cont...)
› Debugging capabilities

› Interpreted language

› Extensive parsing capabilities

2/16/2011 5
Components of REXX

› Instructions
› Built-in functions
› TSO functions
› Data stack functions

2/16/2011 6
Instruction
› Keyword
± Tells the language processor to do something
› Assignment
± Gives a value to a variable or changes the current
value of a variable
› Label
± A symbolic name followed by a colon
± Identifies a portion of the exec
± Commonly used in subroutines and functions, and
with the CALL/SIGNAL instruction
2/16/2011 7
Instruction (Cont...)

› Command (both REXX commands and host


commands)

2/16/2011 8
Built-in functions

› These functions are built into the language


processor
› Provide convenient processing options

2/16/2011 9
TSO external functions

› Interact with the system


› Do specific tasks for REXX

2/16/2011 10
Data stack functions

› Store data for I/O


› Other types of processing

2/16/2011 11
2/16/2011 12

Syntax of REXX
Character Type of REXX
› A REXX instruction can be in lower case, upper
case, or mixed case
› Alphabetic characters are changed to uppercase,
unless enclosed in single or double quotation
marks
› The two types of quotation marks cannot be
mixed
› If any word in the statement is a variable, REXX
substitutes the value

2/16/2011 13
Format
› REXX uses a free format
› A line usually contains one instruction except
when it ends with a comma (,) or contains a
semi-colon (;).
± Comma is the continuation character
± Indicates that the instruction continues to the next line
± Semi-colon indicates the end of the instruction
± Used to separate multiple instructions on one line

2/16/2011 14
Environment / Address

› ADDRESS TSO for native TSO commands


› ADDRESS ISPEXEC for ISPF services
› ADDRESS ISREDIT for ISPF edit macros
› These are required to invoke the environment for
function calls

2/16/2011 15
2/16/2011 16

Variables and expressions


Variables

› Character or group of characters that represents a


value
e.g. count = 1000
› Variable names can consist of:
± A....Z / a - Z alphabetic
± 0....9 numbers
±@#$¢?!._ special characters

2/16/2011 17
Variables (Cont...)

› Restrictions on the variable name are:


± The first character cannot be 0 through 9 or a period
(.)
± The variable name cannot exceed 250 bytes
± The variable name should not be RC, SIGL, or
RESULT, which are REXX special variables

2/16/2011 18
Parsing
› Separates data by comparing the data to a
template (or pattern of variable names)
› Preserves the case of the input data
› PARSE UPPER converts data to uppercase
› Separators in a template can be
± blank,
± string,
± variable, or
± number that represents column position

2/16/2011 19
Parsing

› Blank - an example
± Each variable name gets one word of data in
sequence except for the last, which gets the remainder
of the data
± PARSE VALUE µValue with Blanks.¶ WITH
pattern type
‡ pattern contains µValue¶
‡ type contains µ with Blanks.¶

2/16/2011 20
Parsing

› Blank - another example


± PARSE VALUE µValue with Extra Variables.¶
WITH data1 data2 data3 data4 data5
‡ data1 contains µValue¶
‡ data2 contains µwith¶
‡ data3 contains µExtra¶
‡ data4 contains µVariables.¶
‡ data5 contains µ¶

2/16/2011 21
Parsing

› Substitution - an example
± PARSE VALUE µValue with Periods in it.¶ WITH
pattern . type .
± pattern contains µValue¶
± type contains µPeriods¶
± the periods replace the words ³with´ and ³in it.´

2/16/2011 22
Parsing

› Separators - an example
± phrase = µDun & Bradstreet¶
± PARSE VAR phrase part1 µ&¶ part2
± part1 contains µDun ¶
± part2 contains µ Bradstreet¶

2/16/2011 23
Parsing

› Number: Numbers in a template to indicate


the column at which data must be separated
± Unsigned integer indicates an absolute column
position and
± Signed integer indicates a relative column
position

2/16/2011 24
Parsing
› Absolute column position
± An unsigned integer or an integer prefixed with an
equal sign (=) in a template
± The first segment starts at column 1 and goes up to,
but does not include, the information in the column
number specified
± The subsequent segments start at the column
numbers specified

2/16/2011 25
Parsing

› Absolute column position - an example


± quote = µDun & Bradstreet¶
± PARSE VAR quote part1 6 part2
‡ part1 contains µDun &¶
‡ part2 contains µBradstreet¶

2/16/2011 26
Parsing

› Absolute column position - another example


± quote = µDun & Bradstreet¶
± PARSE VAR quote part1 5 part2 7 part3 1 part4
± part1 contains µDun¶
± part2 contains µ&¶
± part3 contains µBradstreet¶
± part4 contains µDun & Bradstreet¶

2/16/2011 27
Parsing

› Relative column position


± A signed integer in a template separates the data
according to relative column position
± The starting position is relative to the starting
position of the preceding part.
± Can be either positive (+) or negative (-)

2/16/2011 28
Parsing

› Relative column position - an example


± quote = µDun & Bradstreet¶
± PARSE VAR quote part1 +5 part2 +5 part3
± part1 contains µDun &¶
± part2 contains µ Brad¶
± part3 contains µstreet¶

2/16/2011 29
Parsing
› Variables
± Define and use variables to provide further flexibility
of a PARSE VAR instruction
± Define the variable prior to the parse instruction
± Enclose the variable in parenthesis - this variable
must be an unsigned integer
± Use a sign outside the parenthesis to indicate how
REXX is to interpret the unsigned integer
± REXX substitutes the numeric value for the variable
2/16/2011 30
Parsing
› Variables - an example
± quote = µDun & Bradstreet¶
± movex = 4
± PARSE VAR quote part5 +6 part6 +4 part7
-(movex) part8
± part5 contains µDun &¶
± part6 contains µBrad¶
± part7 contains µstreet¶
± part8 contains µBradstreet¶
2/16/2011 31
Expressions

› Something that needs to be calculated/evaluated


› Consists of numbers, variables, or strings, and
one or more operators
› Four types of operators
± Arithmetic
± Comparison
± Logical and
± Concatenation
2/16/2011 32
Arithmetic Operators

› Work on valid numeric constants or on variables


that represent valid numeric constants
‡ + Add
‡ - Subtract
‡ -number Negate the number
‡ +number Add the number to 0

2/16/2011 33
Arithmetic Operators (Cont...)

±* Multiply
± ** Raise a number to a whole number power
± / Divide
±% Divide and return a whole number without a
remainder (quotient only)
± // Divide and return the remainder only

2/16/2011 34
Arithmetic Operators -
Priority

› Priority from maximum to minimum


±-+ Prefix operators
± ** Power (exponential)
± * / % // Multiplication and division
±+- Addition and subtraction

2/16/2011 35
Comparison operators
› Do not return a number value
› Return either a true or false response in terms of
1 or 0 respectively
± == Strictly Equal
±= Equal
±> Greater than
±< Less than
± >= Greater than or equal to
± <= Less than or equal to

2/16/2011 36
Comparison operators (Cont...)

± \== Not strictly equal


± \= Not equal
± >< Greater than or less than (same as not equal)
± \< Not less than
± \> Not greater than

2/16/2011 37
Strictly Equal and Equal Operators

› When two expressions are strictly equal,


everything including the blanks and case (when
the expressions are characters) is exactly the
same
› When two expressions are equal, they are
resolved to be the same

2/16/2011 38
Logical Operators
› Return a true (1) or false (0) value when
processed
› Combine two comparisons and return the true (1)
or false (0) value depending on the results of the
comparisons
› Used in complex conditional instructions
› Can act as checkpoints to screen unwanted
conditions

2/16/2011 39
Logical Operators (Cont...)
› The logical operators are
±& AND
Returns 1 if both comparisons are true
±| Inclusive OR
Returns 1 if at least one comparison is true
± && Exclusive OR
Returns 1 if only one comparison (but not both) is
true
± Prefix \ Logical NOT
Returns the opposite response
2/16/2011 40
Concatenation operators

› Combine two terms into one


› Terms can be strings, variables, expressions, or
constants
› Concatenation can be significant in formatting
output

2/16/2011 41
Concatenation operators (Cont...)

› blank concatenate terms, one blank in between


e.g. TRUE BLUE result is TRUE BLUE
› || concatenate terms, no blanks in between
e.g. ³a´||´.b´ result is a.b
› abuttal concatenate terms, no blanks in between
e.g. per_centµ%¶ if per_cent = 50, result
is 50%

2/16/2011 42
Overall Operator Priority
›\ ¬ - + Prefix operators
› ** Power (exponential)
› * / % // Multiply and divide
›+ - Add and subtract
› blank || abuttal Concatenation
operators
› == = >< Comparison operators
›& Logical AND
› | && inclusive OR, exclusive OR
2/16/2011 43
2/16/2011 44

Control of
program flow
Conditional instructions
› Instructions which set up at least one condition in
the form of an expression

› IF/THEN/ELSE, and

› SELECT/WHEN/OTHERWISE

2/16/2011 45
IF construct
› Can direct the execution of an exec to one of two
choices
› IF expression
THEN instruction
ELSE instruction
› for more than one instruction for a condition,
begin the set of instructions with a DO and end
them with an END

2/16/2011 46
IF construct (Cont...)
› IF expression THEN
DO
instruction
instruction
END
ELSE
DO
instruction
instruction
END
2/16/2011 47
SELECT construct
› can direct the execution to one of many choices.
› SELECT
WHEN expression THEN instruction
WHEN expression THEN instruction
:
OTHERWISE
instruction(s)
END

2/16/2011 48
SELECT construct

› for more than one instruction for a possible path,


begin the set of instructions with a DO and end
them with an END

› However, if more than one instruction follows


the OTHERWISE keyword, DO and END are
not necessary

2/16/2011 49
Looping instructions
› Tell the language processor to repeat a set of
instructions
› A loop can repeat a specified number of times or
› Can use a condition to control repeating
› Two types of loops
± Repetitive repeat instructions a certain number of
times
± Conditional use a condition to control repeating

2/16/2011 50
Repetitive loops
› Repeat a set of instructions a specified number of
times
± e.g. DO i = 1 to 5
SAY ³Hello !´
END
› The step can also be controlled
e.g. DO i = 1 to 10 STEP 2
SAY ³Hello !´
END
2/16/2011 51
Repetitive loops
› DO FOREVER/END - infinite loops
EXIT when a condition is reached
› LEAVE instruction - leaves the loop
e.g. DO FOREVER
IF X = 0 THEN LEAVE

2/16/2011 52
Conditional loops

› DO WHILE
± DO WHILE expression
instruction(s)
END
± Test the expression before the loop executes the first
time and repeat only when the expression is true

2/16/2011 53
Conditional loops

› DO UNTIL
± DO UNTIL expression
instruction(s)
END
± Test the expression after the loop executes at least
once and repeat only when the expression is false

2/16/2011 54
Interrupt instructions
› Tell the language processor to leave the exec
entirely or
› leave one part of the exec and go to another part
either permanently or temporarily
› These are
± EXIT
± SIGNAL
± CALL/RETURN

2/16/2011 55
EXIT

› Causes an exec to unconditionally end


› Return to where the exec was invoked
› Can also return a value to the caller of the
exec

2/16/2011 56
SIGNAL label

› Interrupts the normal flow of an exec


› Causes control to pass to a specified label
› Unlike CALL, SIGNAL does not return to a
specific instruction to resume execution
› When SIGNAL is issued from within a loop, the
loop automatically ends

2/16/2011 57
CALL / RETURN

› When calling an internal subroutine, CALL


passes control to a label specified after the CALL
keyword
› When the subroutine ends with the RETURN
instruction, the instructions following CALL are
executed

2/16/2011 58
CALL / RETURN

› When calling an external subroutine, CALL


passes control to the exec name that is specified
after the CALL keyword
› When the external subroutine completes, the
RETURN instruction returns to where you left
off in the calling exec

2/16/2011 59
2/16/2011 60

Functions and Subroutines


Functions
› Sequence of instructions that can
± Receive data
± Process that data, and
± Return a value
› All functions return a value to the exec that
issued the function call
› Syntax is function(arguments)
› No space between the function name and the left
parenthesis
2/16/2011 61
Functions - parameters
› Up to 20 arguments separated by commas
± Blank function( )
± Constant function(55)
± Symbol function(symbol_name)
± Literal function(µWith a literal string¶)
± function(function(arguments))
Another function
± function(µWith a literal string¶, 55, option)
Combination of argument types
2/16/2011 62
Functions - Types
› Built-in functions built into the language
processor
› User-written functions --
± Written by an individual user or supplied by an
installation
± Internal function is part of the current exec that starts
at a label
± External function is a self-contained program or exec
outside of the calling exec
2/16/2011 63
Functions - Types

› User-written functions ± Example


/*REXX*/
f=INIT_RTN(2,5)
say f /* it displays the result of user written
function */
EXIT
INIT_RTN:
arg a,b
c=a+b
RETURN c

2/16/2011 64
2/16/2011 65

Built-in functions
Arithmetic functions
› ABS Returns the absolute value of the
input number
› MAX Returns the largest number from
the list
› MIN Returns the smallest number from
the list specified

2/16/2011 66
Arithmetic functions

ABS('12.3') -> 12.3


ABS(' -0.307') -> 0.307
MAX(-7,-3,-4.3) -> -3
MAX(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1
7,18,19,MAX(20,21)) -> 21
MIN(12,6,7,9) -> 6
MIN(17.3,19,17.03) -> 17.03

2/16/2011 67
Arithmetic functions
› RANDOM() Returns a quasi-random, non-
negative whole number in the range specified

› SIGN Returns a number that indicates the


sign of the input number

› TRUNC Returns the integer part of the input


number, and optionally a specified number of
decimal places
2/16/2011 68
Arithmetic functions
SIGN('12.3') -> 1
SIGN(' -0.307') -> -1
SIGN(0.0) -> 0

TRUNC(12.3) -> 12
TRUNC(127.09782,3) -> 127.097
TRUNC(127.1,3) -> 127.100
TRUNC(127,2) -> 127.00

2/16/2011 69
Comparison functions
› COMPARE
± Returns 0 if the two input strings are identical
± Returns the position of the first character that does
not match

› DATATYPE Returns a value indicating data


type of the input string, such as a number or
character

› SYMBOL Returns this state of the symbol


(variable,
2/16/2011
literal, or bad) 70
Comparison functions

COMPARE(ACC,ACD) /* 3 */
DATATYPE(123) /* NUM OR CHAR */
A.3=5; J=3 ;
SYMBOL('J') /* VAR */
SYMBOL('K') /* LIT */
SYMBOL('A.J') /* VAR */
SYMBOL('552') /* LIT CONSTANT SYMBOL */
SAY SYMBOL('+') /* BAD NOT A VALID SYMBOL */

2/16/2011 71
Conversion functions

› Convert one type of data representation to


another type of data representation
› B2X Binary to hexadecimal
› C2D Character to Decimal
› C2X Character to Hexadecimal
› D2C Decimal to Character

2/16/2011 72
Conversion functions

› D2X Decimal to Hexadecimal


› X2B Hexadecimal to binary
› X2C Hexadecimal to Character
› X2D Hexadecimal to Decimal

2/16/2011 73
Conversion functions
M= B2X(0101) ; SAY M /* BINARY TO HEXADECIMAL 5 */
M= C2D(ABC); SAY M /* CHARACTER TO DECIMAL 12698307 */
M= C2X(ABC); SAY M /* CHARACTER TO HEXADECIMAL C1C2C3 */
M= D2C(123); SAY M /* DECIMAL TO CHARACTER # */
M= D2X(123); SAY M /* DECIMAL TO HEXADECIMAL 7B */
M = X2B(ABC); SAY M /* HEXADECIMAL TO BINARY 101010111100 */
M = X2C(ABC); SAY M /* HEXADECIMAL TO CHARACTER :: */
M = X2D(ABC); SAY M /* HEXADECIMAL TO DECIMAL 2748 */

2/16/2011 74
Formatting functions
› CENTER/CENTRE
Returns a string of a specified length with the
input string centered in it, with pad characters
added as necessary to make up the length

› COPIES Returns the specified number of


concatenated copies of the input string

› FORMAT Returns the input number, rounded


and formatted to the number of digits specified
2/16/2011 75
Formatting functions
CENTER(ABC,5) /* ' ABC ' */
CENTER(ABC,10,'-') /* '---ABC----' */
CENTRE(ABC,9,'-') /* '---ABC---' */
COPIES(µ-¶,6) ý ------
FORMAT('1.73',4,3) ý ' 1.730'
FORMAT('-.76',4,1) ý ' -0.8'
FORMAT('3.03',4) ý ' 3.03'
FORMAT(' - 12.73',,4) ý '-12.7300'
FORMAT(' - 12.73') ý '-12.73'

2/16/2011 76
Formatting functions

› JUSTIFY Returns a specified string


formatted by adding pad characters between
words to justify to both margins

› LEFT Returns a string of the


specified length, truncated or padded on the right
as needed

2/16/2011 77
Formatting functions

› RIGHT Returns a string of the


specified length, truncated or padded on the left
as needed

› SPACE Returns the words in the


input string with a specified number of pad
characters between each word

2/16/2011 78
Formatting functions
ë 

 





 

ë 

 






ë 

 





ë 

 



  

 




 


 




 


 

 



 

 

 





 



 
 




 

 



 

!  

 





 
 

! 

 
 "



 

 

!  

 





 
 

!  

 

 



  

!  

 





  

2/16/2011 79
String manipulating functions
› ABBREV Returns a string indicating if
one string is equal to the specified number of
leading characters of another string

› DELSTR Returns a string after


deleting a specified number of characters,
starting at a specified point in the input string

› DELWORD Returns a string after


deleting a specified number of words, starting at
a2/16/2011
specified word in the input string 80
String manipulating functions
ABBREV('PRINT','PRI',3) -> 1
ABBREV('PRINT','PRI') -> 1
ABBREV('PRINT','PRI',4) -> 0
ABBREV('PRINT','PRY') -> 0
ABBREV('PRINT','',1) -> 0

DELSTR('ABCD',3) -> 'AB'


DELSTR('ABCDE',3,2) -> 'ABE'
DELSTR('ABCDE',6) -> 'ABCDE'

DELWORD('NOW IS THE TIME',2,2) -> 'NOW TIME'


DELWORD('NOW IS THE TIME ',3) -> 'NOW IS '
DELWORD('NOW IS THE TIME',5) -> 'NOW IS THE TIME'
DELWORD('NOW IS THE TIME',3,1) -> 'NOW IS TIME'
2/16/2011 81
String manipulating functions
› FIND Returns the word number of
the first word of a specified phrase found within
the input string

› INDEX Returns the character


position of the first character of a specified
string found in the input string

› INSERT Returns a character string


after inserting one input string into another
string
2/16/2011 after a specified character position 82
String manipulating functions
FIND('NOW IS THE TIME','IS THE TIME') -> 2
FIND('NOW IS THE TIME',' THE') -> 3
FIND('NOW IS THE TIME',' HE') -> 0

INDEX('abcdef','cd') -> 3
INDEX('abcdef','xd') -> 0
INDEX('abcabc','bc',3) -> 5
INDEX('abcabc','bc',6) -> 0

INSERT(' ','abcdef',3) -> 'abc def'


INSERT('123','abc',5,6) -> 'abc 123 '
INSERT('123','abc',5,6,'+') -> 'abc++123+++'
INSERT('123','abc') -> '123abc'
INSERT('123','abc',,5,'-') -> '123--abc'
2/16/2011 83
String manipulating functions
› LASTPOS Returns the starting character
position of the last occurrence of one string in
another

› LENGTH Returns the length of the


input string

› OVERLAY Returns a string that is the


target string overlaid by a second input string

2/16/2011 84
String manipulating functions
LASTPOS(' ','abc def ghi') -> 8
LASTPOS(' ','abcdefghi') -> 0
LASTPOS('xy','efgxyz') -> 4
LASTPOS(' ','abc def ghi',7) -> 4

LENGTH('abcdefgh') -> 8
LENGTH('abc defg') -> 8
LENGTH('') -> 0

OVERLAY(' ','abcdef',3) -> 'ab def'


OVERLAY('.','abcdef',3,2) -> 'ab. ef'
OVERLAY('qq','abcd') -> 'qqcd'
OVERLAY('qq','abcd',4) -> 'abcqq'
OVERLAY('123','abc',5,6,'+') -> 'abc+123+++'
M=OVERLAY('D',,
OVERLAY('C',,
OVERLAY('B','A',15),25),35)
2/16/2011 85
String manipulating functions
› POS Returns the character
position of one string in another

› REVERSE Returns a character string,


the characters of which are in reverse order
(swapped end for end)

› STRIP Returns a character string


after removing leading or trailing characters or
both from the input string
2/16/2011 86
String manipulating functions
POS('day','Saturday') -> 6
POS('x','abc def ghi') -> 0
POS(' ','abc def ghi') -> 4
POS(' ','abc def ghi',5) -> 8

REVERSE('ABc.') -> '.cBA'


REVERSE('XYZ ') -> ' ZYX'

STRIP(' ab c ') -> 'ab c'


STRIP(' ab c ','L') -> 'ab c '
STRIP(' ab c ','t') -> ' ab c'
STRIP('12.7000',,0) -> '12.7'
STRIP('aabbca',,a) -> 'bbc'

2/16/2011 87
String manipulating functions
› SUBSTR Returns a portion of the
input string beginning at a specified character
position

› SUBWORD Returns a portion of the


input string starting at a specified word number

› TRANSLATE Returns a character string


with each character of the input string translated
to another character or unchanged
2/16/2011 88
String manipulating functions
SUBSTR('abc',2) -> 'bc'
SUBSTR('abc',2,4) -> 'bc '
SUBSTR('abc',2,6,'.') -> 'bc....'

SUBWORD('Now is the time',2,2) -> 'is the'


SUBWORD('Now is the time',3) -> 'the time'
SUBWORD('Now is the time',5) -> ''

TRANSLATE('abbc','&','b') -> 'a&&c'


TRANSLATE('abcdef','12','ec') -> 'ab2d1f'
TRANSLATE('abcdef','12','abcd','.') -> '12..ef'
TRANSLATE('APQRV',,'PR')
2/16/2011
-> 'A Q V' 89
String manipulating functions
› VERIFY
Returns a number indicating whether an input
string is composed only of characters from
another input string or returns the character
position of the first unmatched character

› WORD Returns a word from an input


string as indicated by a specified number

› WORDINDEX Returns the character


position in an input string of the first character in
the specified word
2/16/2011 90
String manipulating functions
VERIFY('123','1234567890') -> 0
VERIFY('1Z3','1234567890') -> 2
VERIFY('AB4T','1234567890') -> 1
VERIFY('AB4T','1234567890','M') -> 3 /*Match */
VERIFY('AB4T','1234567890','N') -> 1
VERIFY('1P3Q4','1234567890',,3) -> 4
VERIFY('123','',N,2) -> 2
VERIFY('ABCDE','',,3) -> 3
VERIFY('AB3CD5','1234567890','M',4) -> 6

WORD('a b c',2) -> 'b'

WORDINDEX('Now is the time',3) -> 8


WORDINDEX('Now is the time',6) -> 0
2/16/2011 91
String manipulating functions
› WORDLENGTH Returns the length of a
specified word in the input string

› WORDPOS Returns the word number of


the first word of a specified phrase in the input
string

› WORDS Returns the number of words


in the input string
2/16/2011 92
String manipulating functions
WORDLENGTH('Now is the time',2) -> 2
WORDLENGTH('Now comes the time',2) -> 5
WORDLENGTH('Now is the time',6) -> 0

WORDPOS('the','now is the time') -> 3


WORDPOS('The','now is the time') -> 0
WORDPOS('is the','now is the time') -> 2
WORDPOS('is the','now is the time') -> 2

WORDS('Now is the time') -> 4


WORDS(' ') -> 0
2/16/2011 93
Date formats

› Syntax of the date function id DATE(option)


± A variety of options are available
± The full option, or it¶s first alphabet, can be passed as
argument
± A list of options follows

2/16/2011 94
Date formats
› Base(or Basedate) Returns the number of
complete days (that is, not including the current
day) since and including the date, January 1,
0001, in the format: dddddd (no leading zeros or
blanks)
± The expression DATE(µB¶)//7 returns a number in the
range 0-6, where 0 is Monday and 6 is Sunday
± Thus, this function can be used to determine the day
of the week independent of the language

2/16/2011 95
Date formats
› Century Returns the number of days,
including the current day, since and including
January 1 of the last year that is a multiple of 100
in the format: ddddd (no leading zeros)
Example: A call to DATE(C) is made on March
13, 1992, so the number of days from January 1,
1900, to March 13, 1992, (33675) is returned

› Days Returns the number of days,


including the current day, so far in this year in
the format: ddd (no leading zeros or blanks) 96
2/16/2011
Date formats
› European Returns date in dd/mm/yy format

› Julian Returns date in yyddd format

› Month Returns full English name of the


current month, for example, August

› Normal Returns date in the format:


dd mon yyyy. This is the default
2/16/2011 97
Date formats
› Ordered Returns date in the
format: yy/mm/dd (suitable for sorting)

› Standard / Sorted Returns date in the


format: yyyymmdd (suitable for sorting)

› USA Returns date in


mm/dd/yy format

› Weekday Returns the English name for


the day of the week, in mixed case, for example,
Tuesday
2/16/2011 98
Time formats

› Syntax of the Time function is TIME(option)


± A variety of options are available
± The full option, or it¶s first alphabet, can be passed as
the argument
± A list of options follows

2/16/2011 99
Time formats
› Civil
± Returns the time in Civil format: hh:mmxx
± The hours take the values 1 through 12
± The minutes take the values 00 through 59
± The minutes are followed immediately by the letters
am or pm
± The hour has no leading zero
± The minute field shows the current minute (rather
than the nearest minute) for consistency with other
TIME results
2/16/2011 100
Time formats
› Elapsed
± Returns sssssssss.uuuuuu, the number of
seconds.microseconds since the elapsed-time clock
was started or reset
± No leading zeros or blanks
± Setting of NUMERIC DIGITS does not affect the
number
± The fractional part always has six digits

2/16/2011 101
Time formats

› Hours
± Returns up to two characters
± Gives the number of hours since midnight
± Format is hh
± No leading zeros or blanks, except for a result of 0

2/16/2011 102
Time formats

› Long
± Returns time in the format: hh:mm:ss.uuuuuu
(uuuuuu is in microseconds)
± The first eight characters of the result follow the same
rules as for the Normal form
± The fractional part is always six digits

2/16/2011 103
Time formats

› Minutes
± Returns up to four characters
± Gives the number of minutes since midnight
± Format is mmmm
± No leading zeros or blanks, except for a result of 0

2/16/2011 104
Time formats
› Normal
± This is the default
± Returns the time in the format hh:mm:ss
± Hours take the values 00 through 23
± Minutes and seconds take 00 through 59
± All these are always two digits
± Any fractions of seconds are ignored (times are never
rounded up)

2/16/2011 105
Time formats
› Reset
± Returns sssssssss.uuuuuu, the number of
seconds.microseconds since the elapsed-time clock
was started or reset
± Also resets the elapsed-time clock to zero
± The number has no leading zeros or blanks
± Setting of NUMERIC DIGITS does not affect the
number
± The fractional part always has six digits
2/16/2011 106
Time formats

› Seconds
± Returns up to five characters
± Gives the number of seconds since midnight
± Format is sssss
± No leading zeros or blanks, except for a result of 0

2/16/2011 107
2/16/2011 108

Subroutines
Subroutines
› Series of instructions that an exec invokes
› Performs a specific task
› The subroutine is invoked by the CALL
instruction
› When the subroutine ends, it returns control to
the instruction that directly follows the
subroutine call
› The instruction that returns control is the
RETURN instruction
2/16/2011 109
Subroutines
› Subroutines may be
± Internal and designated by a label, or
± external and designated by the member name that
contains the subroutine

› IMPORTANT NOTE
± Internal subroutines generally appear after the main
part of the exec. So, when there is an internal
subroutine, it is important to end the main part of the
exec with the EXIT instruction
2/16/2011 110
Using subroutines
› Sharing information can be done by

± Passing variables

± Passing arguments

2/16/2011 111
Passing Variables
› Main exec and subroutine share the same
variables by name

› Value of the variable is the same irrespective of


where it has been set

2/16/2011 112
An example
NUMBER1 = 5
NUMBER2 = 10
CALL SUB1
SAY ANSWER
(DISPLAYS 15)
EXIT
SUB1:
ANSWER = NUMBER1
+ NUMBER2
RETURN
2/16/2011 113
Shielding Variables

› Done by using PROCEDURE instruction


immediately after the subroutine label
› All variables used in the subroutine become local
to the subroutine
› Shielded from the main part of the exec

2/16/2011 114
An example
NUMBER1 = 10
CALL SUB2
SAY NUMBER1 NUMBER2
(DISPLAYS 10 NUMBER2)
EXIT

SUB2: PROCEDURE
NUMBER1 = 7
NUMBER2 = 5
RETURN
2/16/2011 115
Exposing Variables

› To protect specific variables


± use the EXPOSE option with the
PROCEDURE instruction followed by the
variables that are to remain exposed to the
subroutine

2/16/2011 116
An example
NUMBER1 = 10
CALL SUB3
SAY NUMBER1 NUMBER2
(DISPLAYS 7 NUMBER2)
EXIT
SUB3: PROCEDURE
EXPOSE NUMBER1
NUMBER1 = 7
NUMBER2 = 5
2/16/2011
RETURN 117
Passing Arguments
› Passed in main EXEC by
± CALL subroutine_name argument1, argument2,
argument3, etc.
± Up to 20 arguments can be passed

› Received in subroutine by
± ARG arg1, arg2, arg3, etc.
± The names of the arguments on the CALL and the
ARG instructions need not be the same
± information is passed by position, and not by name
2/16/2011 118
An example
LENGTH = 10
WIDTH = 7
CALL SUB4 LENGTH, WIDTH
SAY µTHE PERIMETER IS µ RESULT
µMETERS¶
EXIT
SUB4:
ARG LEN, WID
PERIM = 2 * ( LEN + WID)
RETURN PERIM
2/16/2011 119
Compound variables

› Compound variable is a single-dimensional


array, denoted by <variable name>.
› The variable name itself can be more than one
level
› The rules that apply to a variable name also
apply to a compound variable
› Very useful in array manipulation, input/output
etc.
2/16/2011 120
Compound variables
ð 
ð 
ð 



   
 
  
ð 
ð 

ð  !
ð "#$

   
 
ð  %
ð &'
ð ()

ð & !&*'*(**)  


 
ð &'"#$
ð &'( %
ð &'(&'
ð &'((()

2/16/2011 121
2/16/2011 122

Using REXX
Conventions

› Datasets are named as


<high level qualifier>.REXX.EXEC
› The last level .EXEC is optional
› Each member must have the first line as
/* REXX */
› This makes the command processor (TSO)
invoke the REXX interpreter

2/16/2011 123
Concatenations

› Datasets containing REXX EXECs must be


concatenated to SYSEXEC or SYSPROC

› Concatenation to SYSEXEC makes execution


faster, as it is above SYSPROC in the search
order

2/16/2011 124
Execution

› How and where can REXX execs be


executed
± From TSO READY prompt (if no ISPF
services are used)
± From inside ISPF
± From inside another exec

2/16/2011 125
Online Execution

› From ISPF command line prompt


TSO EXEC µXXX.XXX.REXX(MYREXX)¶ EXEC
› From ISPF Command Shell(Option 6 in primary
panel)
EXEC µXXX.XXX.REXX(MYREXX)¶ EXEC
› VIEW XXX.XXX.REXX
Command ===>
Name Prompt
Õ Õ_____ MYREXX
_________ MYREX2

2/16/2011 126
Batch Execution
66ë# 
66!

$ 
!%& ë  
66!'

# &(

66!

# &(

66# 


# &(

66'


(

$
)$$$$$$ $$% $$)
66(

2/16/2011 127
Data stack

› Place for storing variables temporarily


› Manipulated using PUSH and QUEUE
› No limits on the length and format of the data
items
› Theoretically no limit on the number of items to
be stacked - limited only by practical
considerations

2/16/2011 128
Data Stack
   O   

 


 




  
  
  





O  
O  
O  
O  
O  

O  

2/16/2011 129
Data stack
› PUSH works LIFO (Last-In-First-Out)
± puts items to the top of the stack

› QUEUE works FIFO (First-In-First-Out)


± puts items on the bottom of the stack

› In both cases, elements are removed by PULL


› QUEUED() gives the number of items put on a
stack in a single EXEC
2/16/2011 130
Data Stack - example
6( $$(6

*
%

*
%

+' , +

*
%"

6(#(6

*
%

! 

%

6( #(6

!


!


!


!


!


+  +

!


+  +

!


$

2/16/2011 131
Input/Output processing

› Operations with datasets


› All datasets with a defined record format
are allowed (except RECFM=U)

2/16/2011 132
DISKR
› EXECIO DISKR for reading the dataset/member
± ³EXECIO 0 DISKR mydd (OPEN´ - just opens the
dataset
± ³EXECIO 25 ...´- reads 25 lines
± ³EXECIO * ...´ - reads all the lines
± ³EXECIO * DISKR myindd 100 (FINIS´ - reads
all lines from line 100
‡ In all the above cases, the lines are read on to the STACK
± ³EXECIO * DISKR myindd (STEM newvar.´ -
reads into a stem of variables called newvar
2/16/2011 133
DISKW

› Options for DISKR also hold for DISKW


› Writes to dataset/member
› Can be written from stem/stack
› Numerous other options available for
positioning, skipping, LIFO/FIFO etc.

2/16/2011 134
Example
› Use REXX to
± List all datasets following a particular pattern
± For each dataset, list all the members starting with a
particular pattern

› This can be done by


± Using LMDLIST to get the dataset list
± Then using LMMLIST on each dataset to get the
member list
± Match patterns, and display appropriate members
2/16/2011 135
Example:
6( $$(6


'
'#

6('&+%#- +

'#&+!$$%#- +

(6

%'.'/


'.'

#
ë&
#
% %


%'.'

$ %!&!% %ë

$

'% &!#  44++44$ %!44++

'.'/

'%
&
++
44
'%
44
++

#!
'!

#'% &'#44++44$ %!44++

#!
% %

#'%
&
++
44
#'%
44
++

!#  &'

#!
'!

!#  &++!#  ++

+ #

''! 
'+'% +

  +

 
#

+ $ #
(

 
'!
 %
'!
'+

&# !+% %++(+

+
''! +

+ 
+0123  +
% % +

+ #

'#'! 
'+#'% +


&# !+#+

  +


% %

+ $ #
(

 ,
#'!
 %
'!
'+

  '
+
'#'! +

#!
'!

'

  '

2/16/2011 136
Debugging REXX Execs
› Trace ?I (Intermediates)
± Stops execution after each instruction
› Trace ?R (Results)
± Displays the result of execution of each step, but runs
continuously
› These help to trace the progress of the EXEC
› EXECUTIL TS (Trace start) and TE (trace end)
can also be used to start and end the tracing of a
REXX
2/16/2011 137
Debugging REXX Execs

› RC Return code of the last instruction executed


› SIGL Set to the line number from where the
transfer occurred
› These variables can also be used to trace the exec

2/16/2011 138
TSO External functions

› All TSO commands can be issued


› Enclose them within quotation marks
± e.g. "LISTDS µmy.dataset¶ STATUS"
› Variables can also be passed
± e.g. "LISTDS" name "STATUS"
± here, name is a variable used in the REXX

2/16/2011 139
LISTDS µXXX.XXX.REXX' STATUS

XXX.XXX.REXX
--RECFM-LRECL-BLKSIZE-DSORG--DDNAME---DISP
FB 80 27920 PO SYS00007 KEEP
--VOLUMES--
TPT047

2/16/2011 140
2/16/2011 141

ISPF functions
Panels

› Display panels, accept data and pass on data for


processing
› Syntax is
± ADDRESS ISPEXEC
± ³Display panel(XXXXXX)´

2/16/2011 142


!  $
' '
# # #,

!  $
' '
# #!' 

!  $
' '
# # 

! # ! 
' '
# # *

!  $
' '
# #, 

! (

!  $
' ' #,
# # 
   -  

! '! 
' ' #,
# # *

 

!  $
' ' #,

!  $
' '
# #, 

'  #

#%%'
&&.!%

(
%'!'
(

:
 /9 

:
 /9

5 

;

:
% /9%

6#!#'

&&.#

$ :

7
!
# 
' 

 :

7
!
ë#
' 

% 

'

!  #&#

<!%&

<#&

!#

-! 
#
!#

- <#'  "

 '

2/16/2011 143
)ATTR
Panel example
@ TYPE(OUTPUT) INTENS(HIGH)
# TYPE(OUTPUT) INTENS(LOW) COLOR(YELLOW)
* TYPE(TEXT) INTENS(LOW) COLOR(GREEN) HILITE(REVERSE)
TYPE(OUTPUT) INTENS(LOW) COLOR(TURQ)
% TYPE(TEXT) INTENS(LOW) COLOR(TURQ) HILITE(USCORE)
+ TYPE(TEXT) INTENS(LOW)
)BODY
+-----------------* ARRAY OF VARIABLES +--------------------------+
+COMMAND ==>_ZCMD +SCROLL ==>_SAMT +
++
+JOBNAME1 STEP1 PGM1 DDN1 DSN1 DISP1
)MODEL
@JOBNAME1 #STEP1 PGM1 @DDN1 #DSN1 DISP
)INIT
&SAMT=CSR
&ZCMD=''
)END
2/16/2011 144
Tables

› All table services like TBCREATE, TBADD,


TBUPDATE, TBDELETE etc.
› Syntax is
± ADDRESS ISPEXEC
± ³TBADD tbl-name ....´
± ³TBDELETE tbl-name...´ etc.

2/16/2011 145
ISPF Tables - Syntax
+! $ 
  
! 
 
=$$$$$$+
+

+! $ 
  
!
 
=$$$$$$+
+

# %'
&
+




+

+! $ 
 
+ %!++

+! $ 
  
+ %!+
'% +# %'+
, +

+! $ 
 #! '
= %!+
'#, +

&%

+! $ 
 
= %!+
' $
'% #' *+

+! $ 
 '
= %!++

+! $ 
 

+ %!++

+! $ 
 ##%
+ %!++

+! $ 
  #
+ %!++

+! $ 
 #! '

+ %!+
'#, +

+! $ 
 !
+ %!+
!' !' +

+! $ 
  !
+ %!+

+! $ 
  #
+ %!++
2/16/2011 146
Skeletons
› ISPF Skeleton services like generating JCLs
from skeletons
› Syntax is
± ADDRESS ISPEXEC
± ³FTOPEN´
± ³FTINCL SKELNAME´
± ³FTCLOSE TEMP´
± ADDRESS TSO
± ³SUBMIT ZTEMPF´
2/16/2011 147
File Tailoring Example:
/*REXX*/
"ISPEXEC LIBDEF !  DATASET ID('PXX.XXX.SKEL')
UNCOND"
!%&+ ë +
"ISPEXEC FTOPEN TEMP"
"ISPEXEC FTINCL !"
"ISPEXEC FTCLOSE"
"ISPEXEC VGET (ZTEMPF)"
SAY ZTEMPF
"SUBMIT '"ZTEMPF"'"
EXIT

2/16/2011 148
File Tailoring Example:
Skeleton ! :
//SKELJCL JOB ('245T01',53),'SKELJCL',CLASS=T,
// MSGCLASS=T,REGION=0M,NOTIFY=&SYSUID
//*
//ISPF1 EXEC PGM=<!%
//SYSPRINT DD SYSOUT=*
//SYSTSPRT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//SYSTSIN DD *
EX µXXX.XXX.REXX(MYREXX)'
//*

2/16/2011 149
ISREDIT Macros

› Edit a dataset from program for required


processing
› Syntax is
± ADDRESS ISPEXEC
± "EDIT DATASET(µXXX.XXX.XXX¶)
MACRO(MYMACRO)"

2/16/2011 150
Macro Example:

/*REXX*/
ARG DSNAME1
ADDRESS ISPEXEC "EDIT DATASET('"DSNAME1"')
MACRO(%#)"
<< INSTRUCTION >>
<< INSTRUCTION >>
<< INSTRUCTION >>
EXIT

2/16/2011 151
Macro Example:
%#
/*REXX*/
ADDRESS ISPEXEC
"ISREDIT MACRO"
"ISREDIT FIND FIRST 'ENVIRONSYSTEM'"
"ISREDIT X ALL ' ' 47 70 "
"ISREDIT DEL ALL X"
"ISREDIT C ALL ' ' 87 90 '****'"
"ISREDIT SAVE"
"ISREDIT END"
"ISREDIT MEND"

2/16/2011 152
Messages:

› Pop messages to the panel


› Syntax is
± "SETMSG MSG(MSGXXnn) COND"

2/16/2011 153
Message Example

% $$/
"ISPEXEC LIBDEF ISPMLIB DATASET ID(³XXX.XXX.MESG") "
If SYSDSN(PPP.PPP.PP) /= µOK¶ THEN
³ISPEXEC SETMSG MSG(%  ) ³

% :
%   'NO DATASET' .ALARM=YES
µDATA SET IS NOT CATALOGUED ON THE DEVICE'

MSG010B 'NO MEMBER IN DATASET' .ALARM=YES


µMEMBER NOT FOUND IN THE DATA SET ON THE DEVICE'

2/16/2011 154
LM Functions

› Dataset list
› Member list
± Copy members
± Delete members
± manipulate statistics of members

2/16/2011 155
LMDLIST - Example
/*REXX*/
LVL = USERID()
"ISPEXEC LMDINIT LISTID(ABC) LEVEL("LVL")"
"ISPEXEC LMDLIST LISTID("ABC") OPTION(SAVE)
STATS(YES) GROUP(%)"
/* THE OUTPUT WILL BE IN HLQ.%.DATASETS*/
"ISPEXEC LMDFREE LISTID("ABC")"
EXIT

2/16/2011 156
LMMLIST - Example
/*REXX*/
DSN="µXXX.XXX.LIST'"
"ISPEXEC LMINIT DATAID(ABC) DATASET("DSN") "
"ISPEXEC LMOPEN DATAID("ABC")"
"ISPEXEC LMMLIST DATAID("ABC") OPTION(SAVE)
STATS(YES) GROUP(AMD)"
/* THE OUTPUT WILL BE IN HLQ.AMD.MEMBERS */
"ISPEXEC LMFREE DATAID("ABC")"
EXIT

2/16/2011 157
LMCOPY - Example
/*REXX*/
ARG DSNI DSNO
ADDRESS TSO
"ISPEXEC LMINIT DATASET('"DSNI"') DATAID(INP) ENQ(SHR)"
"ISPEXEC LMINIT DATASET('"DSNO"') DATAID(OUT)
ENQ(EXCLU)"
"ISPEXEC LMCOPY FROMID("INP")
FROMMEM(MEM1)
TODATAID("OUT")
TOMEM(MEM2) REPLACE"
EXIT

2/16/2011 158
References

› TSO/E Version 2 REXX/MVS User¶s Guide


› TSO/E Version 2 REXX/MVS Reference
› Both these books are available on our file server
(IBM Book Manager for Windows)
› Programming in REXX by Charles Daney,
J.Ranade IBM series

2/16/2011 159
2/16/2011 160

Thank
You!

You might also like