I'm working on rewriting a lengthy Rexx script into a Python program and I am trying to figure out the best way to emulate the functionality of a Rexx compound variable. Would a dictionary be the best bet? Obviously, a dictionary will behave differently and won't be exactly the same as a compound variable.
3 Answers
Rexx-Stem variable and python-dictionaries are similar but there are differences. Considder creating a RexxStem class based on a dictionary
Simple Stem expressions
a.b
can be translated to python as
a[b]
Compound Stem expressions
From my experience
a.b.c.d
would be translated to python as
a[b + '.' + c + '.' + d]
Try running the following rexx with your current interpretter and see what you get:
a.2.3 = 'qwerty'
zz = 2'.'3
say a.zz
in some rexx interpreters you would get 'qwerty'. Not sure if that is all
Initializing a Stem Variables
in rexx you can initialize a stem variable lic
a. = 'abc'
Some common uses are
no = 0
yes = 1
found. = no
if ... then do
found.v = yes
end
....
if found.y = yes then do
..
end
or
counts. = 0
do while ...
if ... then do
counts.v = counts.v + 1;
end
end
Initial Value of a stem variable
Like all Rexx variables, the default/initial value of a variable so the default value of a.2.3 is A.2.3. If you are coming from another language this may seem strange but it can be quite handy in debugging - if a variable name pops up unexpectedly --> you have not initiated. It also means numeric expressions crash if you do not initialize a variable.
This not something you need to implement, just be aware of.

- 10,358
- 1
- 27
- 38
-
That makes a lot of sense, I see now how the two structures differ from each other, I will definitely look into creating a class to emulate that! – kbarnum Jun 28 '19 at 12:13
-
1Yes, `a.b.c.d` would be translated to Python as `a[b + '.' + c + '.' + d]`, with the _caveat_ that if `b`, `c`, or `d` aren't set, their values would be "B", "C", and "D" respectively. That's true for any Rexx variable. – Ross Patterson Jun 29 '19 at 21:18
-
"`a.2.3 = 'qwerty' ; zz = 2'.'3; say a.zz` in some rexx interpreters you would get 'qwerty'. Not sure if that is all" - Yes, this is correct Rexx behavior. Any Rexx implementation should produce that result. `2'.'3` means "concatenate 2, '.', and 3, and assign the result to zz", "a.2.3" means "the element of a named "2.3"', and "a.zz" means "the elemnt of a named with the value of zz". – Ross Patterson Jun 29 '19 at 21:25
-
"default value of a.2.3 is A.2.3" - More correctly, the default value of any simple variable is its name in uppercase, the tail of any compound variable is the concatenation of all its elements as simple variables with the dots intact, and the value of an unset compound variable is either the default value of the stem, or the uppercased concatenation of the stem and tail with the dot intact if there is no default set. – Ross Patterson Jun 29 '19 at 21:30
Python dictionaries and Rexx stems are both associative arrays. They differ a bit in how they behave. Rexx's rules are very simple:
- An array reference is split into the "stem" and the "tail", separated by a single dot.
- The stem is a variable name, case-independently. This is the dictionary.
- The tail is processed to identify an element of the array. It is split into one or more dot-separated substrings. Each substring is treated as a variable: if there is a variable with that case-independent name, its value is used instead of its name. Otherwise the name is uppercased and used. The string is put back together, dots and all. This is the key.
- The array can have a default value, set by
stem. = value
, which applies to all unset elements.
So, the result of a an array reference stem.tailpart1.tailpart2.tailpart3
in Python is:
def evaluate_tail(tail, outer_locals):
result = []
for element in tail.split('.'):
if element in outer_locals:
result.append(str(outer_locals[element]))
else:
result.append(str(element).upper())
return '.'.join(result)
array_default_value = 4
stem = {'A.B.C': 1, 'A.9.C': 2, 'A..q': 3}
b = 9
d = 'q'
tail1 = 'a.b.c'
tail2 = 'a..b'
tail3 = 'a..d'
stem.get(evaluate_tail(tail1,locals()), array_default_value) # 'stem.a.b.c' >>> stem['A.9.C'] >>> 2
stem.get(evaluate_tail(tail2,locals()), array_default_value) # 'stem.a..b' >>> stem['A..9'] (not found) >>> (default value) >>> 4
stem.get(evaluate_tail(tail3,locals()), array_default_value) # 'stem.a..d' >>> stem['A..q'] >>> 3

- 9,527
- 33
- 48
I am not a Python person but I know what a Dictionary is.
Depending on how complex the Rexx compound variable is, yes.
a.b
...is easily translatable to a dictionary.
a.b.c.d.e.f.g.h
...is less easily translatable to a dictionary. Perhaps a dictionary within a dictionary within a dictionary within a dictionary within a dictionary within a dictionary within a dictionary.

- 10,237
- 1
- 28
- 39
-
1I think both cases can be handled via dictionary a.b ==> a[b] and a.b.c ==>a[ b + '.' c] in python – Bruce Martin Jun 28 '19 at 00:35