title: How do I get the size of an array in Cobol?
body: <p>I want to get the length of a table (by which I mean the number of elements in an array) in COBOL. The convention I have seen is typically to hard-code it to match the occurrences in working storage. But I want the code to get the length, so that if the working storage is changed and the program recompiled, then the procedure division statements do no need to be changed. This is both to reduce maintenance effort, and prevent just "missing" a use in the 5000 lines of code, and potentially to allow the code to be in a copycode that could be used in multiple programs that have different table lengths.</p>
  
  <p>So here is the only solution I have come up with.</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>And this outputs</p>
  
  <pre><code>000000300 / 000000020 = 0015
  </code></pre>
  
  <p>So that works, but it's pretty clunky. It requires a dummy group level around the rows, just to get the length of them. Is there a better way to do this?</p>