REXX Cheat Sheet
REXX Cheat Sheet
Basic Syntax
Program Structure
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 */
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 */
Logical Operators
& /* AND */
| /* OR (inclusive) */
&& /* XOR (exclusive OR) */
\ ¬ /* NOT */
Examples:
Concatenation Operators
Examples:
Data Types
Strings
str1 = 'Single quotes'
str2 = "Double quotes"
str3 = 'You can use ''quotes'' inside' /* Escape with double quotes */
str4 = "He said ""Hello"""
Numbers
Note: All data is stored as strings; REXX converts automatically when needed
Input/Output
Output - SAY
Input - PULL
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'
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
/* Repeat 5 times */
DO 5
SAY 'Hello!'
END
/* Using variable */
count = 5
DO count
SAY 'Hello!'
END
DO - Controlled Loop
/* 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
DO UNTIL
DO FOREVER
DO FOREVER
SAY 'Enter command (QUIT to exit):'
PULL command
IF command = 'QUIT' THEN LEAVE
/* Process command */
END
LEAVE and ITERATE
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
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 */
Examples:
Examples:
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 */
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 */
Multi-dimensional Arrays
/* Two-dimensional array */
table.1.1 = 'A1'
table.1.2 = 'A2'
table.2.1 = 'B1'
table.2.2 = 'B2'
/* Process array */
DO i = 1 TO employee.0
SAY employee.i
END
Array Operations
Parsing
PARSE Instruction Syntax
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)
By Position (Absolute)
/* Absolute positions */
PARSE VALUE '1234567890' WITH 1 a 4 b 7 c 10
/* a='123', b='456', c='789' */
By Position (Relative)
By String Delimiter
/* Parse by string delimiter */
PARSE VALUE 'Name:John,Age:30' WITH 'Name:' name ',' 'Age:' age
/* name='John', age='30' */
Mixed Templates
PARSE VALUE 'John Doe 123 Main St' WITH first last 1 fullname 10 address 20
Parsing Examples
/* Parse date */
PARSE VALUE DATE('S') WITH year +4 month +2 day +2
/* For 20250127: year='2025', month='01', day='27' */
greet:
PARSE ARG name, age
SAY 'Hello,' name '! You are' age 'years old.'
RETURN 'Success'
Calling Functions
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
External Subroutines/Functions
File Operations
EXECIO - File I/O
Allocate File (TSO)
Write to File
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
Close/Free File
Error Handling
Error Conditions
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
error_handler:
SAY 'Error occurred: RC =' RC
SAY 'Command:' CONDITION('D')
RETURN
failure_handler:
SAY 'Failure occurred: RC =' RC
RETURN
CONDITION Function
Stack Operations
Stack Instructions
PUSH 'First'
PUSH 'Second'
PUSH 'Third'
QUEUE 'First'
QUEUE 'Second'
QUEUE 'Third'
Checking Stack
IF QUEUED() > 0 THEN
PULL data
ELSE
SAY 'Stack is empty'
Stack Management
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 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
SIGNAL ON SYNTAX
EXIT
SYNTAX:
SAY 'Syntax error at line' SIGL
SAY 'Source:' SOURCELINE(SIGL)
SAY 'Error:' ERRORTEXT(RC)
EXIT
RC /* Return code */
SIGL /* Source line number */
RESULT /* Result from function/subroutine */
SOURCELINE Function
ERRORTEXT Function
/* Get error message for error number */
msg = ERRORTEXT(40) /* Get message for error 40 */
/* ============================================== */
/* Program: MYPROG */
/* Purpose: Description of what program does */
/* Author: Your Name */
/* Date: 2025-01-27 */
/* ============================================== */
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
Variable Initialization
/* Initialize variables explicitly */
count = 0
total = 0
name = ''
/* Initialize arrays */
data. = ''
data.0 = 0
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.