How to get array size in Cobol?

I want to get the length of the table (by which I mean the number of elements in the array) in COBOL. The agreement that I saw, as a rule, hardcodes it according to the entry in the working repository. But I want the code to get the length, so if the working repository is changed and the program is recompiled, then the instructions for the separation of procedures do not need to be changed. This is both a reduction in maintenance efforts, and the prevention of simply "lack" of use in 5000 lines of code and the potential for using code in copy code that can be used in several programs with different table lengths.

So here is the only solution I came up with.

IDENTIFICATION DIVISION.
PROGRAM-ID. TESTPROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 THIS-LENGTH    PIC 9(04).
01 THIS-GROUP.
   05 THIS-TABLE  PIC X(20) OCCURS 15 TIMES.
PROCEDURE DIVISION.

    COMPUTE THIS-LENGTH = LENGTH OF THIS-GROUP
                         / LENGTH OF THIS-TABLE.
     DISPLAY LENGTH OF THIS-GROUP ' / ' LENGTH OF THIS-TABLE
             ' = ' THIS-LENGTH.
 EXIT-PROG.
     STOP RUN.

And it deduces

000000300 / 000000020 = 0015

, , . , . ?

+3
2

78 OCCURS, , , , ...

:

  WORKING-STORAGE SECTION.
  78 THIS-TABLE-SIZE VALUE 15.

   01 THIS-LENGTH    PIC 9(04).
   01 THIS-TABLE     PIC X(20) OCCURS THIS-TABLE-SIZE TIMES.
   PROCEDURE DIVISION.
      DISPLAY THIS-TABLE-SIZE.

$if.. :

  WORKING-STORAGE SECTION.
  $if THIS-TABLE-SIZE defined
  $display THIS-TABLE-SIZE is changed
  $else
   78 THIS-TABLE-SIZE VALUE 15.
  $end

   01 THIS-LENGTH    PIC 9(04).
   01 THIS-TABLE     PIC X(20) OCCURS THIS-TABLE-SIZE TIMES.
   PROCEDURE DIVISION.
      DISPLAY THIS-TABLE-SIZE.

/ :

Y:\DemoAndTests\size.of>cobol testprog.cbl nologo int();
* Generating testprog
* Data:         800     Code:         464     Literals:         144

Y:\DemoAndTests\size.of>run testprog
15

​​...

Y:\DemoAndTests\size.of>cobol testprog.cbl nologo int() constant"THIS-TABLE-SIZE(20)";
THIS-TABLE-SIZE is changed
* Generating testprog
* Data:         896     Code:         464     Literals:         144

Y:\DemoAndTests\size.of>run testprog
20

78 .

+2

" ". :

05  DELIBERATE-DUMMY-GROUP.
    10  FILLER OCCURS 15 TIMES.
        15  ACTUAL-ENTRY-NAME PIC X(20).

05  SOMETHING-ELSE PIC something.

05  ACTUAL-ENTRY-NAME OCCURS 15 TIMES PIC X(20).
05  SOMETHING-ELSE PIC something.

01  W-ADDRESS-OF-TABLE USAGE POINTER.
01  FILLER REDEFINES W-ADDRESS-OF-TABLE.
    05  W-AOT-AS-NUMBER COMP-5 PIC 9(9).
01  W-ADDRESS-OF-AFTER-TABLE USAGE POINTER.
01  FILLER REDEFINES W-ADDRESS-OF-AFTER-TABLE.
    05  W-AOAT-AS-NUMBER COMP-5 PIC 9(9).

SET W-ADDRESS-OF-TABLE TO ADDRESS OF ACTUAL-ENTRY-NAME ( 1 )
SET W-ADDRESS-OF-AFTER-TABLE TO ADDRESS OF SOMETHING-ELSE

SUBTRACT W-AOT-AS-NUMBER FROM W-AOAT-AS-NUMBER
  GIVING W-LENGTH-OF-TABLE
DIVIDE W-LENGTH-OF-TABLE BY LENGTH OF ACTUAL-ENTRY-NAME
  GIVING W-NO-OF-OCCURENCES

, OCCURS .

. , "SOMETHING-ELSE" . SYNC, ( "" ).

+1

All Articles