Search code examples
associative-arraymainframezosrexx

Is there a way to do associative arrays in REXX?


I have some Perl code (for performance analysis) first developed under Linux which now needs to be ported to the mainframe. Apparently REXX is the scripting language of choice on that platform but this Perl script relies heavily on associative arrays (basically arrays where the index is a string).

Is there a way that in REXX? How would I code up something like:

$arr{"Pax"} = "Diablo";
$arr{"Bob"} = "Dylan";
print $arr{"Pax"} . "\n";
if (defined $arr{"no"}) {
        print "Yes\n";
} else {
        print "No\n";
}

Solution

  • You can use stem variables, not exactly like arrays but very similar

    /* REXX */
    NAME = PAX
    ARRAY.NAME = "DIABLO"
    NAME = BOB
    ARRAY.NAME = "DYLAN"
    NAME = 'PAX'
    SAY "ARRAY.PAX " IS ARRAY.NAME
    NAME = 'BOB'
    SAY "ARRAY.BOB " IS ARRAY.NAME
    NAME = 'SANDY'
    SAY "ARRAY.SANDY " IS ARRAY.NAME
    IF ARRAY.NAME = "ARRAY.SANDY" THEN SAY "ARRAY.SANDY IS EMPTY"
    

    The above Rexx will print

    ARRAY.PAX  IS DIABLO
    ARRAY.BOB  IS DYLAN
    ARRAY.SANDY  IS ARRAY.SANDY
    ARRAY.SANDY IS EMPTY
    

    They can also be compound like a.b.c A stem variable if empty will return itself. There is no way to iterate of a stem that does not use consecutive numbers as the index that I know of.

    IBM Manual with reference to Stem variables

    Perl is available as an extra free feature for ZOS IBM Ported Tools for z/OS