Using "open" compound variables

[Autolink] Menu

If you're using compound variables completely defined in your source code, you may use a technique I call "open compound variables" shown in the example below.
The advantage of this method: You can add new variables to the end of the compound variable without maintaining a counter (stem.0 is not used). I often forget to correct the counter after adding new entries to the compound variable.
Note that using this method in Classic REXX is only useful if you define the compound variable completely in the source code (no changes at runtime)!
In Object-Oriented REXX you can use it anyway because there you can get all stem entries very simply with the DO ... OVER construct (see the code at the end of the example).

 
/* sample for using "open" compound variables                         */

                    /* drop the stem (to be sure)                     */
  drop nameStem.

                    /* define the compound variable                   */
  nameStem.1 = "Albert"
  nameStem.2 = "Robert"
  nameStem.3 = "William"

                    /* use the compound variable                      */

                    /* use the function symbol to find the last entry */
                    /* of the compound variable                       */

  do i = 1 while symbol( "nameStem." || i ) = "VAR"

    say "Entry no. " || i || " is " nameStem.i

  end /* do i = 1 while symbol( "nameStem." || i ) = "VAR" */

                     /* Note: i-1 is the number of stem entries. You  */
                     /*       can save this value in the variable     */
                     /*       nameStem.0 and use the normal methods   */
                     /*       in the rest of your program.            */

  say "The compound variable contains " || i-1 || " entries."

/* another useful method for a loop over all entries            v2.50 */
/* Note that this method is only possible in Object REXX!!!     v2.50 */

   parse version thisVersion .
   if thisVersion = "OBJREXX" then
   do
     say "In Object REXX you can use also the DO OVER construct:"

     j = 0;
     do i over nameStem.
       j = j + 1;
       say "Entry no. " || i || " is " namestem.i
     end /* do i over namestem. */
     say "The compound variable contains " || j || " entries."

  end /* if thisVersion = "OBJREXX" then */

(see also Defining compound variables)


[Back: Defining compound variables]
[Next: Initializing compound variables]