Search code examples
initializationcobol

Initialize variable-length in Cobol


I have a copybook with the folowing:

(...)
05 ESTGWABC-S-OUT.
   10 ESTGWABC-S-COD-NUM        PIC 9(003).
   10 ESTGWABC-S-DESC-COD       PIC X(020).
   (...)
   10 ESTGWABC-S-VAL-PAY        PIC 9(015)V99.
   10 ESTGWABC-S-QTD-REG        PIC 9(002).
   10 ESTGWABC-S-REG-PEOP    OCCURS 0 TO 20 TIMES
                 DEPENDING ON ESTGWABC-S-QTD-REG.
      15 ESTGWABC-S-CCONTR      PIC 9(009).
      15 ESTGWABC-S-VAL-PAY     PIC 9(015)V99.
   10 ESTGWABC-S-DEPEN          PIC 9(005).
   (...)

On my program, I'm wanting to initialize it before use it, so I'm doing the following:

INITIALIZE                  ESTGWABC-S-OUT
                            REPLACING ALPHANUMERIC BY SPACES
                                           NUMERIC BY ZEROS

But I'm getting an compiling error:

"ESTGWABC-S-OUT" was found in an "INITIALIZE" statement but was variable-length or variably located. The operand was discarded from the "INITIALIZE" statement.

Can anybody give me a clue how can I solve it or what am I doing wrong? Thank you very much.


Solution

  • Can anybody give me a clue how can I solve it or what am I doing wrong?

    Do not use INITIALIZE and you are doing nothing wrong.

    Basically, standard COBOL sets rules for the organization of data records. It then defines the behavior of the INITIALIZE statement to properly operate on those data records.

    The data items after the variable length table are 'variably located'. This does not conform to standard COBOL, which requires that any variable length data item, in this case, ESTGWABC-S-REG-PEOP, be located last in the record description entry. The location of ESTGWABC-S-DEPEN will change depending on the number of entries in the table, ESTGWABC-S-REG-PEOP. When the size of the table changes ESTGWABC-S-DEPEN will no longer be an initialized data item.

    To allow the use of INITIALIZE, the 'copybook' must be changed.


    Following is an example of how to use INITIALIZE with a standard-conforming variable length record. This was done with a Micro Focus compiler with flags to force COBOL 85 conformance.

      $set ans85 flag"ans85" flagas"s"
       identification division.
       program-id. var-len.
       data division.
       working-storage section.
       01  n pic 9(2).
    
       01  a.
        02  fixed-part.
         03  b pic x(2).
         03  c pic 9(2).
        02  variable-part.
         03  d occurs 0 to 10 depending c.
          04  e pic x(2).
          04  f pic 9(2).
    
       procedure division.
       begin.
           initialize fixed-part
           perform varying n from 1 by 1 until n > 10
               initialize d (n)
           end-perform
           stop run
           .
       end program var-len.