Search code examples
cobol

How to print the iteration inside an array (table) in COBOL?


I'm trying to print only one of the elements of this COBOL table, but it seems I'm having trouble in understanding how arrays (tables) in COBOL work!

data division.
       working-storage section.
       01 m-airlines.
         05 m-united occurs 3 times.
           10 m-u-a330 pic 9(1) value 3.
           10 m-u-a321 pic 9(1) value 1.
           10 m-u-a300 pic 9(1) value 5.
         05 m-delta occurs 3 times.
           10 m-dl-a330 pic 9(2) value 35.
           10 m-dl-a321 pic 9(2) value 27.
           10 m-dl-b777 pic 9(2) value 20.

       procedure division.
           display "number of Delta's Airbus a330: " m-delta(1,1).

I get this error:

Too many subscripts.


Solution

  • These are the tables your code generates. This isn't what you intented.

                 +-------------+-------------+-------------+
    m-united(1): | m-u-a330(1) | m-u-a321(1) | m-u-a300(1) |
                 +-------------+-------------+-------------+
    m-united(2): | m-u-a330(2) | m-u-a321(2) | m-u-a300(2) |
                 +-------------+-------------+-------------+
    m-united(3): | m-u-a330(3) | m-u-a321(3) | m-u-a300(3) |
                 +-------------+-------------+-------------+
    
                +--------------+--------------+--------------+
    m-delta(1): | m-dl-a330(1) | m-dl-a321(1) | m-dl-b777(1) |
                +--------------+--------------+--------------+
    m-delta(2): | m-dl-a330(2) | m-dl-a321(2) | m-dl-b777(2) |
                +--------------+--------------+--------------+
    m-delta(3): | m-dl-a330(3) | m-dl-a321(3) | m-dl-b777(3) |
                +--------------+--------------+--------------+
    

    Perhaps, this is what you want:

    01 AIRLINE-TABLE.
       05 M-U-A330     PIC 99 VALUE 3.
       05 M-U-A321     PIC 99 VALUE 1.
       05 M-U-A300     PIC 99 VALUE 5.
       05 M-U-B777     PIC 99 VALUE 0.
       05 M-D-A330     PIC 99 VALUE 35.
       05 M-D-A321     PIC 99 VALUE 27
       05 M-D-A300     PIC 99 VALUE 0.
       05 M-D-B777     PIC 99 VALUE 20.
    01 FILLER REDEFINES AIRLINE-TABLE.
       05 AIRLINE OCCURS 2 TIMES.
          10 NUM-A330  PIC 99.
          10 NUM-A321  PIC 99.
          10 NUM-A300  PIC 99.
          10 NUM-B777  PIC 99.
    

    which generates:

                +-------------+-------------+-------------+-------------+
    airline(1): | num-a330(1) | num-a321(1) | num-a300(1) | num-b777(1) |
                +-------------+-------------+-------------+-------------+
    airline(2): | num-a330(2) | num-a321(2) | num-a300(2) | num-b777(2) |
                +-------------+-------------+-------------+-------------+
    
    ... DISPLAY "Number of Delta's Airbus a330 : " num-a330(2)
    

    Or this:

    01 AIRLINE-TABLE.
       05 M-U-A330     PIC 99 VALUE 3.
       05 M-U-A321     PIC 99 VALUE 1.
       05 M-U-A300     PIC 99 VALUE 5.
       05 M-U-B777     PIC 99 VALUE 0.
       05 M-D-A330     PIC 99 VALUE 35.
       05 M-D-A321     PIC 99 VALUE 27
       05 M-D-A300     PIC 99 VALUE 0.
       05 M-D-B777     PIC 99 VALUE 20.
    01 FILLER REDEFINES AIRLINE-TABLE.
       05 AIRLINE OCCURS 2 TIMES.
          10 AIRPLANE OCCURS 4 TIMES.
             15 NUM-PLANES PIC 99.
    

    which gives

                +------------------+------------------+------------------+------------------+
    airline(1): | num-planes(1, 1) | num-planes(1, 2) | num-planes(1, 3) | num-planes(1, 4) |
                +------------------+------------------+------------------+------------------+
    airline(2): | num-planes(2, 1) | num-planes(2, 2) | num-planes(2, 3) | num-planes(2, 4) |
                +------------------+------------------+------------------+------------------+
    
    ... DISPLAY "Number of Delta's Airbus a330 : " num-planes(2, 1)