Would a Python dictionary be the equivalent data structure to a Rexx compound variable?

296 views Asked by At

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

There are 3 answers

0
Ross Patterson On BEST ANSWER

Python dictionaries and Rexx stems are both associative arrays. They differ a bit in how they behave. Rexx's rules are very simple:

  1. An array reference is split into the "stem" and the "tail", separated by a single dot.
  2. The stem is a variable name, case-independently. This is the dictionary.
  3. 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.
  4. 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
1
cschneid On

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.

4
Bruce Martin On

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.