Sunday, November 20, 2011

Multi Dimensional Arrays


COBOL74 supports array of maximum of three dimensions whereas COBOL85 supports up to seven dimensions. The lowest- level OCCURS data-name or an item subordinate to it is used to access an entry in the array or the table.
If we use SEARCH for accessing multi-dimension table, then INDEXED BY must be used on all OCCURS levels. Expanded nested perform is available for processing multi level tables. The syntax of this perform is given below:

PERFORM para-1 thru para-n
  VARYING index-1 from 1 BY 1 UNTIL index-1 > size- of- outer-occurs
  AFTER VARYING index-2 from 1 by 1 until index-2 > size of inner occurs.

SEARCH example for multi level tables:
01 EMP-TABLE.
          05 DEPTNUMBER OCCURS 10 TIMES INDEXED BY I1.
                   10 EMP-DETAIL OCCURS 50 TIMES INDEXED BY I2.
                             15 EMP-NUMBER  PIC 9(04).
                             15 EMP-SALARY   PIC 9(05).
77 EMPNUMBER-IN                              PIC 9(04) VALUE ‘2052’.
PERFORM 100-SEARCH-EMP-SAL VARYING I1 FROM 1 BY 1
     UNTIL  I1 > 10 OR WS-FOUND 
100-SEARCH-EMP-SAL.
          SET I2 TO 1.
          SEARCH EMP-DETAIL AT END DISPLAY ‘NOT FOUND’     == > Lowest Occurs                                   
              WHEN EMPNUMBER-IN = EMP-NUMBER(I1,I2)
                          DISPLAY ‘SALARY IS:’ EMP-SALARY(I1,I2)
      SET WS-FOUND TO TRUE                             == > Search ends
END-SEARCH.

No comments:

Post a Comment