0% found this document useful (0 votes)
45 views31 pages

REXX Cheat Sheet

Uploaded by

Abhishek Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views31 pages

REXX Cheat Sheet

Uploaded by

Abhishek Das
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

REXX Programming

Language Cheat Sheet


Table of Contents
1. Basic Syntax
2. Comments
3. Variables
4. Operators
5. Data Types
6. Input/Output
7. Control Structures
8. Loops
9. Functions
10. String Manipulation
11. Arrays (Compound Variables)
12. Parsing
13. Subroutines and Functions
14. File Operations
15. Error Handling
16. Stack Operations
17. Debugging

Basic Syntax
Program Structure

/* REXX programs MUST start with a comment on line 1 */


/* This tells the interpreter it's a REXX program */

SAY 'Hello, World!'


EXIT 0

Key Rules
Free-form language: No column restrictions, case-insensitive
Instructions: One per line (or use ; to separate multiple on one line)
Continuation: Use , at end of line to continue on next line
No semicolons required at end of statements

Comments
/* Single line comment */

/* Multi-line
comment
block */

SAY 'Hello' /* Inline comment */

/* Comments can span


multiple lines
like this */

Note: REXX only supports /* */ style comments (no // or #)

Variables
Variable Assignment

name = 'John'
age = 25
total = num1 + num2

Variable Rules
No declaration needed - variables are created on assignment
Case-insensitive: Name, NAME, and name are the same
Uninitialized variables default to their name in uppercase
No typing: All variables are strings (converted as needed)

Special Variables
RC /* Return code from last command */
RESULT /* Value returned from subroutine */
SIGL /* Line number of last branch */

Operators
Arithmetic Operators

+ /* Addition */
- /* Subtraction */
* /* Multiplication */
/ /* Division */
% /* Integer division (quotient) */
// /* Remainder (modulo) */
** /* Power (exponentiation) */

Examples:

a = 7 + 2 /* 9 */
b = 7 - 2 /* 5 */
c = 7 * 2 /* 14 */
d = 7 / 2 /* 3.5 */
e = 7 % 2 /* 3 (integer division) */
f = 7 // 2 /* 1 (remainder) */
g = 7 ** 2 /* 49 (power) */

Comparison Operators

= /* Equal to */
\= ¬= /* Not equal to */
< /* Less than */
> /* Greater than */
<= =< /* Less than or equal to */
>= => /* Greater than or equal to */

Note: Returns 1 for true, 0 for false

Logical Operators
& /* AND */
| /* OR (inclusive) */
&& /* XOR (exclusive OR) */
\ ¬ /* NOT */

Examples:

result = (a > 5) & (b < 10) /* Both conditions must be true */


result = (a = 1) | (b = 2) /* At least one must be true */
result = \(a = b) /* Negate condition */

Concatenation Operators

|| /* Concatenate (no space) */


/* Space concatenates with one blank */

Examples:

full = first || last /* 'JohnDoe' */


full = first last /* 'John Doe' (with space) */
full = first' 'last /* 'John Doe' (explicit space) */

Operator Precedence (Highest to Lowest)


1. Prefix operators: +, -, \
2. Power: **
3. Multiplication/Division: *, /, %, //
4. Addition/Subtraction: +, -
5. Concatenation: ||, blank
6. Comparison: =, \=, <, >, <=, >=
7. Logical AND: &
8. Logical OR: |
9. Logical XOR: &&

Data Types
Strings
str1 = 'Single quotes'
str2 = "Double quotes"
str3 = 'You can use ''quotes'' inside' /* Escape with double quotes */
str4 = "He said ""Hello"""

Numbers

whole = 123 /* Integer */


decimal = 123.45 /* Decimal */
scientific = 1.23E+5 /* Scientific notation (123000) */
negative = -45

Note: All data is stored as strings; REXX converts automatically when needed

Input/Output
Output - SAY

SAY 'Hello, World!'


SAY 'The answer is' result
SAY variable1 variable2 variable3

Input - PULL

SAY 'Enter your name:'


PULL name /* Converts to UPPERCASE */

SAY 'Enter your age:'


PULL age

Input - PARSE PULL (Case-sensitive)

SAY 'Enter your name:'


PARSE PULL name /* Preserves original case */

PARSE UPPER PULL name /* Converts to uppercase (same as PULL) */


Input - ARG (Command-line arguments)

/* Call: REXX myprog.rexx John 25 Developer */

ARG name age profession /* Gets arguments (UPPERCASE) */


PARSE ARG name age profession /* Gets arguments (preserves case) */

Control Structures
IF-THEN-ELSE

/* Simple IF */
IF age >= 18 THEN
SAY 'You are an adult'

/* IF-ELSE */
IF weather = 'rainy' THEN
SAY 'Take an umbrella'
ELSE
SAY 'Enjoy the sunshine'

/* IF-ELSE with multiple statements */


IF score >= 60 THEN DO
SAY 'Passed!'
grade = 'P'
END
ELSE DO
SAY 'Failed!'
grade = 'F'
END

/* Single line format */


IF condition THEN instruction; ELSE instruction

/* Using NOP for no operation */


IF condition THEN
instruction
ELSE
NOP
Nested IF

IF score >= 90 THEN


grade = 'A'
ELSE IF score >= 80 THEN
grade = 'B'
ELSE IF score >= 70 THEN
grade = 'C'
ELSE
grade = 'F'

SELECT-WHEN-OTHERWISE

SELECT
WHEN grade = 'A' THEN
SAY 'Excellent!'
WHEN grade = 'B' THEN
SAY 'Good'
WHEN grade = 'C' THEN
SAY 'Average'
WHEN grade = 'D' THEN
SAY 'Below Average'
OTHERWISE
SAY 'Failed'
END

/* With multiple statements */


SELECT
WHEN age < 13 THEN DO
status = 'Child'
discount = 50
END
WHEN age < 20 THEN DO
status = 'Teen'
discount = 25
END
OTHERWISE DO
status = 'Adult'
discount = 0
END
END
Loops
DO - Simple Repetition

/* Repeat 5 times */
DO 5
SAY 'Hello!'
END

/* Using variable */
count = 5
DO count
SAY 'Hello!'
END

DO - Controlled Loop

/* Basic controlled loop */


DO i = 1 TO 10
SAY 'Number' i
END

/* With BY increment */
DO i = 1 TO 10 BY 2
SAY i /* Prints: 1, 3, 5, 7, 9 */
END

/* Counting down */
DO i = 10 TO 1 BY -1
SAY i
END

/* Using variables */
start = 1
finish = 100
step = 10
DO i = start TO finish BY step
SAY i
END
DO WHILE

/* Tests condition at TOP of loop */


num = 1
DO WHILE num <= 5
SAY num
num = num + 1
END

/* Won't execute if condition initially false */


x = 10
DO WHILE x < 5
SAY 'This will never print'
END

DO UNTIL

/* Tests condition at BOTTOM of loop */


/* Executes at least once */
num = 1
DO UNTIL num > 5
SAY num
num = num + 1
END

/* Executes once even if condition is true */


x = 10
DO UNTIL x > 5
SAY 'This prints once'
x = x + 1
END

DO FOREVER

DO FOREVER
SAY 'Enter command (QUIT to exit):'
PULL command
IF command = 'QUIT' THEN LEAVE
/* Process command */
END
LEAVE and ITERATE

/* LEAVE - Exit loop immediately */


DO i = 1 TO 10
IF i = 5 THEN LEAVE
SAY i /* Prints: 1, 2, 3, 4 */
END

/* ITERATE - Skip to next iteration */


DO i = 1 TO 10
IF i = 5 THEN ITERATE
SAY i /* Prints: 1, 2, 3, 4, 6, 7, 8, 9, 10 */
END

Compound Loops

/* Repetitive + Conditional */
DO i = 1 TO 10 WHILE condition
/* Instructions */
END

DO i = 1 TO 10 UNTIL condition
/* Instructions */
END

Functions
Built-in Arithmetic Functions

ABS(number) /* Absolute value */


MAX(n1, n2, ...) /* Maximum value */
MIN(n1, n2, ...) /* Minimum value */
RANDOM(min, max) /* Random number */
SIGN(number) /* Sign: -1, 0, or 1 */
TRUNC(number, decimals) /* Truncate number */

Examples:
x = ABS(-5) /* 5 */
y = MAX(10, 20, 15) /* 20 */
z = MIN(10, 20, 15) /* 10 */
r = RANDOM(1, 100) /* Random between 1-100 */
s = SIGN(-5) /* -1 */
t = TRUNC(3.14159, 2) /* 3.14 */

Built-in Comparison Functions

COMPARE(str1, str2) /* Returns 0 if equal, else position of mismatch */


DATATYPE(string, type) /* Check data type */

Examples:

result = COMPARE('ABC', 'ABC') /* 0 */


result = COMPARE('ABC', 'ABD') /* 3 */

IF DATATYPE(age, 'N') THEN /* Check if numeric */


SAY 'Valid number'

Built-in Conversion Functions

B2X(binary) /* Binary to Hexadecimal */


C2D(string) /* Character to Decimal */
C2X(string) /* Character to Hexadecimal */
D2C(decimal) /* Decimal to Character */
D2X(decimal) /* Decimal to Hexadecimal */
X2B(hex) /* Hexadecimal to Binary */
X2C(hex) /* Hexadecimal to Character */
X2D(hex) /* Hexadecimal to Decimal */

Built-in Formatting Functions


CENTER(str, len, pad) /* Center string */
CENTRE(str, len, pad) /* British spelling */
COPIES(str, n) /* Replicate string */
FORMAT(number) /* Format number */
LEFT(str, len, pad) /* Left-align string */
RIGHT(str, len, pad) /* Right-align string */
SPACE(str, n, pad) /* Space words */

Examples:

x = CENTER('Hi', 10) /* ' Hi ' */


y = COPIES('*', 5) /* '*****' */
z = LEFT('Hello', 10, '.') /* 'Hello.....' */
w = RIGHT('Hello', 10, '.') /* '.....Hello' */

Built-in Miscellaneous Functions

ADDRESS() /* Current environment */


ARG(n) /* Return argument n */
DATE(format) /* Current date */
TIME(format) /* Current time */
QUEUED() /* Number of items in stack */
TRACE(option) /* Query/set trace */
USERID() /* TSO user ID */

String Manipulation
String Functions
LENGTH(string) /* Length of string */
POS(needle, haystack) /* Position of substring */
LASTPOS(needle, haystack) /* Last position of substring */
REVERSE(string) /* Reverse string */
SUBSTR(string, start, len) /* Extract substring */
WORD(string, n) /* Extract nth word */
WORDS(string) /* Count words */
WORDINDEX(string, n) /* Position of nth word */
WORDLENGTH(string, n) /* Length of nth word */
WORDPOS(phrase, string) /* Find phrase position */

Examples:

len = LENGTH('Hello') /* 5 */
pos = POS('lo', 'Hello') /* 4 */
rev = REVERSE('Hello') /* 'olleH' */
sub = SUBSTR('Hello', 2, 3) /* 'ell' */
w = WORD('One Two Three', 2) /* 'Two' */
n = WORDS('One Two Three') /* 3 */
idx = WORDINDEX('One Two Three', 2) /* 5 */
wlen = WORDLENGTH('Hello World', 1) /* 5 */

String Modification Functions

DELSTR(string, start, len) /* Delete substring */


DELWORD(string, n, count) /* Delete words */
INSERT(new, target, pos) /* Insert string */
OVERLAY(new, target, pos) /* Overlay string */
STRIP(string, option, char)/* Remove leading/trailing chars */
TRANSLATE(string, out, in) /* Translate characters */

Examples:

s = DELSTR('Hello', 2, 2) /* 'Hlo' */
s = DELWORD('One Two Three', 2) /* 'One' */
s = STRIP(' Hello ') /* 'Hello' */
s = TRANSLATE('hello') /* 'HELLO' (to uppercase) */
s = TRANSLATE('hello', 'AEIOU', 'aeiou') /* Replace vowels */

Bit Manipulation Functions


BITAND(string1, string2, pad) /* Logical AND */
BITOR(string1, string2, pad) /* Logical OR */
BITXOR(string1, string2, pad) /* Logical XOR */

Arrays (Compound Variables)


Creating Arrays (Stem Variables)

/* Stem variables consist of: stem.tail */


employee.1 = 'John Smith'
employee.2 = 'Jane Doe'
employee.3 = 'Bob Johnson'

/* Using loop to create array */


DO i = 1 TO 5
PARSE PULL employee.i
END

/* Associative arrays (non-numeric tails) */


person.name = 'John'
person.age = 30
person.city = 'New York'

Multi-dimensional Arrays

/* Two-dimensional array */
table.1.1 = 'A1'
table.1.2 = 'A2'
table.2.1 = 'B1'
table.2.2 = 'B2'

/* Using variables as indices */


DO row = 1 TO 10
DO col = 1 TO 5
table.row.col = row * col
END
END

Initializing Stem Variables


/* Initialize all elements with stem name */
employee. = '' /* Sets default value for all employee.* */

/* Set count in 0 element (convention) */


employee.0 = 3
employee.1 = 'John'
employee.2 = 'Jane'
employee.3 = 'Bob'

/* Process array */
DO i = 1 TO employee.0
SAY employee.i
END

Array Operations

/* Drop entire array */


DROP employee.

/* Drop specific element */


DROP employee.5

/* Check if element exists */


IF SYMBOL('employee.5') = 'VAR' THEN
SAY 'Element exists'

Parsing
PARSE Instruction Syntax

PARSE [UPPER|LOWER] source template

Parsing Sources
PARSE ARG arg1 arg2 arg3 /* Parse arguments */
PARSE PULL var1 var2 var3 /* Parse user input */
PARSE VALUE expr WITH template /* Parse expression */
PARSE VAR varname template /* Parse variable */
PARSE SOURCE system how name /* Parse source info */
PARSE LINEIN var /* Parse line from file */

Parsing Templates
By Words (Blank-delimited)

PARSE VALUE 'John Doe 25' WITH first last age


/* first='John', last='Doe', age='25' */

PARSE VALUE 'One Two Three Four' WITH w1 w2 w3


/* w1='One', w2='Two', w3='Three Four' (remainder) */

/* Using period (.) to discard */


PARSE VALUE 'One Two Three Four' WITH w1 . w3 .
/* w1='One', w3='Three' (Two and Four discarded) */

By Position (Absolute)

/* Absolute positions */
PARSE VALUE '1234567890' WITH 1 a 4 b 7 c 10
/* a='123', b='456', c='789' */

PARSE VALUE 'John Smith 30' WITH 1 name 9 surname 17 age 19

By Position (Relative)

/* Relative positions (+) */


PARSE VALUE 'ABCDEFGH' WITH a +2 b +3 c +3
/* a='AB', b='CDE', c='FGH' */

By String Delimiter
/* Parse by string delimiter */
PARSE VALUE 'Name:John,Age:30' WITH 'Name:' name ',' 'Age:' age
/* name='John', age='30' */

PARSE VALUE 'Field1=Value1;Field2=Value2' WITH 'Field1=' f1 ';' 'Field2=' f2

Mixed Templates

PARSE VALUE 'John Doe 123 Main St' WITH first last 1 fullname 10 address 20

Parsing Examples

/* Parse line with multiple delimiters */


input = 'Name: John Doe | Age: 30 | City: NYC'
PARSE VAR input 'Name:' name '|' 'Age:' age '|' 'City:' city

/* Parse date */
PARSE VALUE DATE('S') WITH year +4 month +2 day +2
/* For 20250127: year='2025', month='01', day='27' */

/* Parse arguments with defaults */


PARSE ARG name ',' age ',' city
IF age = '' THEN age = 0
IF city = '' THEN city = 'Unknown'

Subroutines and Functions


Internal Subroutines
Calling Subroutines
/* REXX Program with subroutine */
CALL greet 'John', 25
SAY 'Returned value:' RESULT
EXIT

greet:
PARSE ARG name, age
SAY 'Hello,' name '! You are' age 'years old.'
RETURN 'Success'

Calling Functions

/* REXX Program with function */


result = square(5)
SAY 'Square is:' result
EXIT

square:
PARSE ARG num
RETURN num * num

Function vs Subroutine
Feature Subroutine Function
Called with CALL name name() or variable = name()
Return value Optional (in RESULT) Required
Return statement RETURN value (optional) RETURN value (required)
Usage Statement by itself Part of expression

PROCEDURE Instruction
/* Protect variables with PROCEDURE */
x = 10
CALL subroutine
SAY x /* Still 10 */
EXIT

subroutine: PROCEDURE
x = 20 /* Local variable */
SAY x /* 20 */
RETURN

/* Expose specific variables */


global_var = 'shared'
CALL subroutine2
EXIT

subroutine2: PROCEDURE EXPOSE global_var


SAY global_var /* Can access global_var */
local = 'hidden'
RETURN

External Subroutines/Functions

/* Call external REXX program */


CALL external_prog arg1, arg2

/* Call external function */


result = external_func(arg1, arg2)

File Operations
EXECIO - File I/O
Allocate File (TSO)

"ALLOC DD(MYFILE) DSN('USER.DATA.FILE') SHR REUSE"

Read from File


/* Read all records to stack */
"EXECIO * DISKR MYFILE (FINIS"

/* Read all records to stem variable */


"EXECIO * DISKR MYFILE (STEM data. FINIS"

/* Read specific number of records */


"EXECIO 10 DISKR MYFILE (STEM data. FINIS"

/* Read one record at a time */


DO WHILE lines > 0
"EXECIO 1 DISKR MYFILE"
PARSE PULL record
SAY record
END

Write to File

/* Write from stack */


PUSH 'Line 1'
PUSH 'Line 2'
PUSH 'Line 3'
"EXECIO 3 DISKW MYFILE (FINIS"

/* Write from stem variable */


data.0 = 3
data.1 = 'Line 1'
data.2 = 'Line 2'
data.3 = 'Line 3'
"EXECIO" data.0 "DISKW MYFILE (STEM data. FINIS"

/* Write single line */


line = 'This is a test line'
"EXECIO 1 DISKW MYFILE (STRING" line

Update File
/* Read, modify, and write back */
"EXECIO * DISKR MYFILE (STEM data. FINIS"

DO i = 1 TO data.0
/* Modify data.i */
data.i = TRANSLATE(data.i) /* Convert to uppercase */
END

"EXECIO" data.0 "DISKW MYFILE (STEM data. FINIS"

Close/Free File

"EXECIO 0 DISKR MYFILE (FINIS" /* Close file */


"FREE DD(MYFILE)" /* Deallocate */

Check EXECIO Return Codes

"EXECIO * DISKR MYFILE (STEM data. FINIS"


IF RC = 0 THEN
SAY 'Success'
ELSE IF RC = 2 THEN
SAY 'End of file'
ELSE IF RC = 4 THEN
SAY 'Empty dataset'
ELSE IF RC = 20 THEN
SAY 'Severe error'

Error Handling
Error Conditions

ERROR /* Command returned error */


FAILURE /* Command failed */
HALT /* External interrupt */
NOVALUE /* Uninitialized variable used */
NOTREADY /* I/O error */
SYNTAX /* Syntax error */
LOSTDIGITS /* Precision lost in arithmetic */
SIGNAL ON - Exit Program

/* Trap errors with SIGNAL (terminates program) */


SIGNAL ON ERROR
SIGNAL ON FAILURE
SIGNAL ON SYNTAX
SIGNAL ON NOVALUE

/* Your code here */


SAY undefined_variable /* Triggers NOVALUE */

EXIT 0

ERROR:
FAILURE:
SAY 'Command error occurred!'
SAY 'RC =' RC
EXIT RC

SYNTAX:
SAY 'Syntax error at line' SIGL
SAY 'Error:' ERRORTEXT(RC)
EXIT RC

NOVALUE:
SAY 'Uninitialized variable at line' SIGL
SAY 'Variable:' CONDITION('D')
EXIT 8

CALL ON - Continue Program


/* Trap errors with CALL (returns to program) */
CALL ON ERROR NAME error_handler
CALL ON FAILURE NAME failure_handler

/* Your code here */


"COMMAND THAT MIGHT FAIL"

SAY 'Program continues...'


EXIT 0

error_handler:
SAY 'Error occurred: RC =' RC
SAY 'Command:' CONDITION('D')
RETURN

failure_handler:
SAY 'Failure occurred: RC =' RC
RETURN

Turn Off Trapping

SIGNAL OFF ERROR


CALL OFF FAILURE

CONDITION Function

/* Get information about trapped condition */


CONDITION('C') /* Condition name */
CONDITION('D') /* Description/details */
CONDITION('I') /* Instruction that raised it */
CONDITION('S') /* Status (ON/OFF/DELAY) */

Check Return Codes


"LISTDS 'USER.DATA.FILE'"
IF RC = 0 THEN
SAY 'Success'
ELSE DO
SAY 'Command failed with RC =' RC
EXIT RC
END

Stack Operations
Stack Instructions

PUSH /* Add to top of stack (LIFO) */


QUEUE /* Add to bottom of stack (FIFO) */
PULL /* Remove from top of stack */
QUEUED() /* Number of items in stack */

Using PUSH (Last In, First Out)

PUSH 'First'
PUSH 'Second'
PUSH 'Third'

PULL item /* item = 'Third' */


PULL item /* item = 'Second' */
PULL item /* item = 'First' */

Using QUEUE (First In, First Out)

QUEUE 'First'
QUEUE 'Second'
QUEUE 'Third'

PULL item /* item = 'First' */


PULL item /* item = 'Second' */
PULL item /* item = 'Third' */

Checking Stack
IF QUEUED() > 0 THEN
PULL data
ELSE
SAY 'Stack is empty'

Stack Management

NEWSTACK /* Create new stack */


/* Work with new stack */
DELSTACK /* Delete new stack */

QSTACK /* Query number of stacks */

Example: Processing Files

/* Read file to stack */


"EXECIO * DISKR MYFILE (FINIS"

/* Process all lines */


DO WHILE QUEUED() > 0
PULL line
SAY line
END

Debugging
TRACE Instruction
Trace Options
TRACE N /* Normal (default, no trace) */
TRACE O /* Off (no trace) */
TRACE A /* All (trace all clauses) */
TRACE C /* Commands (trace commands) */
TRACE E /* Error (trace errors only) */
TRACE F /* Failure (trace failures) */
TRACE I /* Intermediates (trace expressions) */
TRACE R /* Results (trace results of expressions) */
TRACE S /* Scan (check syntax only) */

Interactive Tracing

TRACE ?R /* Interactive trace with results */


TRACE ?I /* Interactive trace with intermediates */

Trace Examples

/* Basic tracing */
TRACE R
x = 5 + 3
SAY x
TRACE O

/* Conditional tracing */
IF debug_mode = 1 THEN
TRACE A

/* Trace as function */
old_trace = TRACE('R') /* Set new and save old */
/* ... code ... */
TRACE VALUE old_trace /* Restore old setting */

Debug Output
/* Display variable values */
SAY 'Debug: x =' x 'y =' y 'z =' z

/* Display at specific points */


SAY 'Entering subroutine with arg:' arg1

/* Trace return codes */


"COMMAND"
SAY 'Command RC =' RC

Using SIGL and SOURCELINE

SIGNAL ON SYNTAX

/* ... code ... */

EXIT

SYNTAX:
SAY 'Syntax error at line' SIGL
SAY 'Source:' SOURCELINE(SIGL)
SAY 'Error:' ERRORTEXT(RC)
EXIT

Special Variables for Debugging

RC /* Return code */
SIGL /* Source line number */
RESULT /* Result from function/subroutine */

SOURCELINE Function

lines = SOURCELINE() /* Total lines in program */


line5 = SOURCELINE(5) /* Get line 5 content */

ERRORTEXT Function
/* Get error message for error number */
msg = ERRORTEXT(40) /* Get message for error 40 */

Common Patterns and Best Practices


Program Template

/* ============================================== */
/* Program: MYPROG */
/* Purpose: Description of what program does */
/* Author: Your Name */
/* Date: 2025-01-27 */
/* ============================================== */

PARSE ARG arguments


SIGNAL ON ERROR
SIGNAL ON FAILURE

/* Main program logic */


CALL process_data arguments

EXIT 0

/* Subroutines below */
process_data:
PARSE ARG data
/* Process data */
RETURN

/* Error handlers */
ERROR:
FAILURE:
SAY 'Error at line' SIGL
SAY 'RC =' RC
EXIT RC

Input Validation
/* Validate numeric input */
PARSE ARG value
IF \DATATYPE(value, 'N') THEN DO
SAY 'Error: Input must be numeric'
EXIT 8
END

/* Validate required arguments */


PARSE ARG required_arg optional_arg
IF required_arg = '' THEN DO
SAY 'Error: Required argument missing'
EXIT 8
END

Safe File Operations

/* Always check return codes */


"ALLOC DD(MYFILE) DSN('USER.DATA') SHR"
IF RC \= 0 THEN DO
SAY 'Allocation failed'
EXIT RC
END

"EXECIO * DISKR MYFILE (STEM data. FINIS"


IF RC \= 0 THEN DO
SAY 'Read failed'
"FREE DD(MYFILE)"
EXIT RC
END

/* Always free files */


"FREE DD(MYFILE)"

Variable Initialization
/* Initialize variables explicitly */
count = 0
total = 0
name = ''

/* Initialize arrays */
data. = ''
data.0 = 0

Quick Reference Table


Common Commands Summary
Category Command Description
I/O SAY Display output
PULL Get input (uppercase)
PARSE PULL Get input (preserve case)
Control IF THEN ELSE Conditional execution
SELECT WHEN Multi-way branch
DO END Block grouping
Loops DO n Repeat n times
DO i = m TO n Controlled loop
DO WHILE Conditional loop (top test)
DO UNTIL Conditional loop (bottom test)
DO FOREVER Infinite loop
LEAVE Exit loop
ITERATE Skip to next iteration
Calls CALL Call subroutine
RETURN Return from routine
EXIT Exit program
Error SIGNAL ON Trap condition (exit)
CALL ON Trap condition (continue)
Debug TRACE Control tracing
Stack PUSH Add to stack top
QUEUE Add to stack bottom
PULL Remove from stack
Files EXECIO File I/O operations
Data DROP Remove variable
PARSE Parse data
Additional Resources
Useful Built-in Functions Quick List
String: LENGTH, POS, SUBSTR, WORD, WORDS, STRIP, TRANSLATE
Numeric: ABS, MAX, MIN, SIGN, TRUNC, RANDOM
Conversion: C2D, C2X, D2C, D2X, X2C, X2D, B2X, X2B
Format: CENTER, LEFT, RIGHT, COPIES, FORMAT
Information: DATATYPE, COMPARE, SYMBOL, ADDRESS, DATE, TIME

Return Code Convention


0 = Success
Non-zero = Error or specific condition
Always check RC after commands

Naming Conventions
Variables: Any case, no special declaration needed
Labels: Alphanumeric, followed by colon :
Stems: Name followed by period .

This cheat sheet covers REXX syntax for mainframe (TSO/E, z/VM) and portable REXX implementations.

Last Updated: October 2025 Version: 1.0

You might also like