开发者

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{"n开发者_如何学Co"}) {
        print "Yes\n";
} else {
        print "No\n";
}


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


I just want to add a bit more to the answer given by Deuian. I agree, REXX stem variables are the answer.

Simple REXX variables default to their own name. For example:

/* REXX */
SAY X

will print "X" until X is assigned some other value:

/* REXX */
X = 'A'
SAY X

will print "A".

No big surprise so far. Stem variables are a bit different. The head of the stem is never evaluated, only the bit after the initial dot is.

To illustrate:

/* REXX */                                                           
X. = 'empty'   /* all unassigned stem values take on this value */
A. = 'nil'
B = 'A'        /* simple variable B is assigned value A */                                                      
X = 'A'        /* simple variable X is assigned value A */                                                      
SAY X.A        /* prints: empty */                                 
X.A = 'hello'  /* Stem X. associates value of A with 'hello' */
SAY X.A        /* prints: hello */                                   
SAY X.B        /* prints: hello */                                   
SAY X.X        /* prints: hello */                                   

Notice the X and the A stem names are not evaluated, however, the X and A variables appearing after them are. Some people find this a bit confusing - think about it for a while and it makes great sense.

The Z/OS version of REXX does not provide a natural way to iterate over a stem variable. The easiest way to do this is to build your own index. For example:

/* REXX */
X. = ''
DO I = 1 TO 10
  J = RANDOM(100, 500) /* Random # 100..500 */
  X.INDEX = X.INDEX J  /* concatinate J's with 1 space between */
  X.J = 'was picked'   /* Associate 'was picked' with whatever J evalauates to */
  END

DO I = 1 TO WORDS(X.INDEX) /* Number of blank delimited 'words' */
  J = WORD(X.INDEX, I)     /* Extract 1 'word' at a time */
  SAY J X.J                /* Print 'word' and its associated value */
  END

Pretty trivial but illustrates the idea. Just be sure that INDEX (or whatever name you choose) to hold the indexing names never pops up as an associative value! If this is a possibility, use some other variable to hold the index.

Last point. Notice each of my examples begins with /* REXX */ you may find that this needs to be the first line of your REXX programs under Z/OS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜