# REXX Coding

**Input-Output Terminal:**

PULL and SAY are the basic I-O statements in REXX.  
Eg: PULL *variable-name,* SAY ‘Hello World’

PARSE command is used for string manipulations  
Eg: PARSE PULL variable-name

Passing Arguments:  
The values which are passed to an exec are usually called arguments.  
Eg: PARSE ARG arg1 arg2 arg3 ……….

TSO EXEC ‘dataset-name-contains-rexx’ - Execution Explicitly  
TSO REXX-MODULE - Execution Implicitly  
  
**User Function:** A block of code which only runs when it is called.  
Eg: SUM(1,2,3)  
  
**Build in Function:** A predefined function that performs a task.  
**Arithmetic Functions:** ABS, MAX, MIN, RANDOM, SIGN, TRUNC  
**Comparison Functions:** COMPARE, DATATYPE  
**Conversion Functions:** B2X, C2D, C2X, D2C, D2X, X2B, X2C, X2D  
**Formatting Functions:** CENTER/CENTRE, COPIES, FORMAT, LEFT, RIGHT, SPACE  
**String Manipulation Functions:** ABBREV, DELSTR, DELWORD, FIND, INDEX, LASTPOS, LENGTH, POS, REVERSE, STRIP, SUBSTR, SUBWORD, WORD, WORDINDEX, WORDLENGHT, WORDPOS, WORDS  
  
**SUBROUTINE:**  
Subroutines are similar to functions which are invoked to perform a specific task. When a subroutine execution is completed successfully, the control returns back to the instruction that follows the subroutine call.  
In many ways subroutines and functions are similar except the way in which they are called and the way values are returned back.  
Eg: CALL SUBR ARG1 ARG2  
  
**Internal Subroutine:** Internal subroutines are coded within the current exec, marked by a label and used only by that exec.  
  
**External Subroutine:** External subroutine is a program or an exec in a member of a partitioned data set that can be called by one or more execs.  
  
**STACK:** FILO  
PUSH VAR1  
PUSH VAR2  
**QUEUE:** FIFO  
QUEUE VAR1  
QUEUE VAR2  
SAY QUEUED() command will return the number of elements in a stack (displays a decimal number).  
  
**Example:**  
/\* REXX \*/  
PUSH a;  
PUSH b;  
'MAKEBUF’ —&gt; Creating Buffer  
PUSH c; —&gt; Moved to MAKEBUF  
PUSH d; —&gt; Moved to MAKEBUF  
'QBUF' —&gt; Number of buffers created  
SAY RC —&gt; Value 1  
'QELEM' —&gt; Number elements in Buffer  
SAY RC —&gt; value 2  
EXIT 0  
  
**Q1) What will be the value stored in 'item'?**  
QUEUE A —&gt; A moved to external QUEUE  
QUEUE B —&gt; B moved to external QUEUE  
'MAKEBUF' —&gt; Create a Buffer  
QUEUE C —&gt; C moved to Buffer  
PULL item —&gt; Item C is pulled because buffer is not closed.  
  
**Q2) What will be the value stored in 'element‘?**  
PUSH 'a' —&gt; A moved to external stack  
PUSH 'b' —&gt; B moved to external stack  
'MAKEBUF' —&gt; Create new Buffer  
PUSH 'c' —&gt; C moved to Buffer  
PUSH 'd' —&gt; D moved to buffer  
‘DROPBUF' —&gt; Buffer dropped  
PARSE PULL element —&gt; B is pulled  
  
**Q3) What will be the value stored in RC?**  
QUEUE A  
QUEUE B  
QUEUE C  
'QELEM'  
SAY RC —&gt; Return 3 because number of elements in QUEUE  
**Q4) What is returned to the function SAY QUEUED ()?** —&gt; Number of elements in QUEUE.  
  
**Q5) Name two instructions that can potentially clear all elements in a stack.**  
DROPBUF (clears only the latest buffer)  
FLUSH (clears the entire stack)  
  
**ADDRESS** command will transfer the control to the respective external ISPF environment. For example, ADDRESS ISPEXEC will transfer the control to ISPF environment.  
  
**FILE OPERATIONS:**  
REXX can perform file operations using TSO commands,  
1) Datasets can be created, allocated, read and written using REXX.  
2) ALLOC command is used to create / allocate datasets.  
3) EXECIO command is used to read and write datasets.  
4) FREE command is used to free the dataset allocation, so that the lock held by the REXX program on the particular dataset used will be released.  
  
**Q1) Rename the input file name with .Bkup as a final qualifier using TSO command.**  
/\* REXX */ /* Performing some basic TSO commands on PS file \*/ SAY "ENTER THE HIGHLEVEL QUALIFIER" PULL HLQ SRCFILE = HLQ||'.SAMPLE.FILE' DSTFILE = HLQ||'.SAMPLE.FILE.BKUP' ADDRESS TSO "RENAME '"SRCFILE"' '"DSTFILE"' " EXIT 0  
  
**Q2) Copy the contents of one file to another, using file operations.**  
/\* REXX\*/  
/ \*COPYING THE DATA FROM ONE FILE TO ANOTHER \*/  
SAY "ENTER THE HIGHLEVEL QUALIFIER"  
PULL HLQ  
INFILE = HLQ||".SAMPLE.REXX"  
OUTFILE = HLQ||".SAMPLE.OUTPUT"  
ADDRESS TSO "  
ALLOC F(INDD) DA('"||INFILE||"') SHR"  
IF SYSDSN("'"OUTFILE"'") = 'OK' THEN  
DO "ALLOC F("OUTDD") DA('"OUTFILE"') SHR REUSE"  
END  
ELSE DO "ALLOC F("OUTDD") DA("||"'"||OUTFILE||"'", ||") NEW SPACE (100,200) TRACK LRECL(80) RECFM(F) ", ||" BLKSIZE(0)"  
END  
"EXECIO DISKR INDD(FINIS STEM INREC."  
"EXECIO \* DISKW OUTDD(FINIS STEM INREC."  
"FREE F(INDD)"  
"FREE F(OUTDD)"  
EXIT 0
