Simplify Bulk Data in Rexx with Stem Variables
Learn how Rexx stem variables let you store and process collections without repetitive code, with a clear example and best‑practice trade‑offs.
15 Jul 2025, 21:12 UTC

Problem: Repetitive Variables in Rexx
When a Rexx script needs to work with a list of items—say, a set of numbers, filenames, or configuration values—it is tempting to declare a separate variable for each element:
num1 = 5
num2 = 7
num3 = 3
As the list grows, the code becomes cluttered, and the pattern is hard to maintain. Adding a new item requires editing every part of the script that references the collection. This is where stem variables shine.
Stem Variables to the Rescue
A stem is a variable name that ends with a dot (e.g., num.). The dot separates the stem name from a suffix that can be a number or any string. The combination behaves like an array element. For example:
num.1 = 5
num.2 = 7
num.3 = 3
Now num. is the stem, and 1, 2, 3 are indexes. You can loop over the indexes without hard‑coding variable names. Stem variables are supported in IBM Enterprise Rexx 5.3+, Regina 4.0+, and many other modern Rexx dialects. If you are using an older Rexx (e.g., 4.x), check the documentation for stem support.
Practical Example – Summing a List of Numbers
Below is a minimal Rexx script that demonstrates how to read a list of numbers, store them as stem elements, and sum them with a single DO loop. The script is intended to run in a standard Rexx interpreter; no external libraries are required.
# sum.rexx
/* Read numbers from command line arguments and sum them */
/* Store each argument in a stem element */
DO i = 1 TO ARGC
num.i = ARG(i)
END
/* Sum the numbers */
total = 0
DO i = 1 TO VARIABLE('num') /* VARIABLE returns the highest numeric suffix */
total = total + num.i
END
/* Output the result */
SAY 'Total = ' total
How to run it:
- Save the script as
sum.rexx. - Open a terminal and invoke the Rexx interpreter:
rexx sum.rexx 5 7 3(replacerexxwith your interpreter’s command). - The output should be:
Total = 15.
Key points:
ARGCandARG(i)are built‑in variables that provide the number of command‑line arguments and each argument’s value.- The
VARIABLE('num')function returns the highest numeric suffix used with the stemnum.. If you hadnum.10, it would return 10. - All arithmetic is performed with standard Rexx numeric types; no special handling is required.
Trade‑offs and Best Practices
While stems eliminate repetitive code, they come with caveats:
- Readability: A single stem with many elements can be harder to understand at a glance, especially if the suffixes are non‑numeric. Keep the stem depth shallow and use meaningful stem names (e.g.,
file.for filenames). - Namespace collisions: If your script inadvertently reuses a stem name in a different context, you may overwrite values. Always choose unique stem names or prefix them with a module identifier.
- Compatibility: Some older Rexx versions treat stems differently or may not support the
VARIABLEfunction. Verify your interpreter’s manual for stem behavior. - Debugging: When printing a stem, Rexx prints only the first element. Use a loop to display all elements if debugging.
Practical guidelines:
- Use stems for logically grouped data that naturally forms a sequence.
- Limit the number of elements to a manageable range (e.g.,
1–100). - When the data set is large or requires complex indexing, consider using a separate data structure (e.g., a file or database) and load it into a stem only for temporary processing.
Takeaway – When to Use Stems
Stem variables are a powerful feature of Rexx that let you treat a set of related values as a single entity. They reduce boilerplate, make loops concise, and keep the script tidy for small to medium collections. However, keep the stem depth shallow, choose clear names, and test on the target interpreter to avoid compatibility surprises. With these practices, stems can become a staple of efficient Rexx scripting.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.