Created On:  09 November 2010

Problem:

How can I declare static arrays with easily modifiable bounds ?

Resolution:

It is often useful to declare arrays with bounds that are easily varied, but not actually dynamic, so that they can be increased (or decreased) globally, without searching through the source code, and trying to catch every reference. Although you can use a normal PL/1 variable to define the bounds of automatic arrays, you cannot do this for static arrays, as the storage is allocated at compilation time, rather than run time. If you want to do something similar with your static arrays, you can use a PreProcessor variable, whose value at precompilation time will be substituted for the variable name, throughout the program.

Note that the technique is not case sensitive, but is more selective than a global edit, as the variable value will only be substituted where the compiler detects a valid variable location. For example, in the following program the string "IM" will be replaced with "200" wherever the context implies the presence of a variable name, but not in the name of the variable LETTER_IM, which is left unchanged, nor in the initial value of the variable BB_LETTER.DATA.

The precompiler can be invoked manually with the command MFPP, or automatically when using the MFPLX command, by passing the "-macro" option.


 STATVAR: procedure options(main);

 dcl sysprint stream print;


 /*
    Normal variable declaration and initialisation
 */
 DCL AU FIXED BIN(15) INIT(155);

 /*
    Automatic structure with dynamic structure array bound
                                     and iteration factor
 */                                     
 DCL 1  AA_LETTER (AU) ,        
        3  LETTER_ID  CHAR (8)  INIT((AU)((8)'A')),          
        3  DATA       CHAR (16) INIT((AU)('AutoDataAutoData'));    


 /*
    PreProcessor variable declaration and assignment
 */
 �L IM CHAR;
 %IM = '200';  

 /*
    Static structure with dynamic element array bound,
                                  and iteration factor
 */                                     
 DCL 1  BB_LETTER STATIC,        
        3  LETTER_ID (IM)  CHAR (8)  INIT((IM)((8)'B')),          
        3  DATA  (IM)      CHAR (16) INIT((IM)('StaticIMStaticIM'));    
 
 /*
    Static structure with dynamic length and repetition factor
 */                                     
 DCL 1  CC_LETTER STATIC,                
        3  LETTER_IM       CHAR (im)  INIT((IM)'C');

 PUT SKIP EDIT ('Array bounds are :- ')(A);
 PUT SKIP EDIT ('AA_LETTER = ',
          HBOUND(AA_LETTER.LETTER_ID,1))(COL(24),A,F(5));
 PUT SKIP EDIT ('BB_LETTER = ',
          HBOUND(BB_LETTER.LETTER_ID,1))(COL(24),A,F(5));

 put skip;

 PUT SKIP DATA(AA_LETTER(1));
 PUT SKIP DATA(AA_LETTER(au));
 put skip;
 PUT SKIP DATA(BB_LETTER.LETTER_ID(1),BB_LETTER.DATA(1));
 PUT SKIP DATA(BB_LETTER.LETTER_ID(im),BB_LETTER.DATA(im));
 put skip;
 PUT SKIP DATA(CC_LETTER.LETTER_IM);

 end STATVAR;