COBOL occurs, Usage is Index, Indexed by are tricky and confused by many people. I already shared important points on COBOL usage is index. The covered points are Index you cannot pass to SUB-PROGRAM and the second point is index is not an Integer.
Most mainframe COBOL programmers confused much here of how to use OCCURS clause with KEY phrase effectively.
Syntax for OCCURS clause with Indexed BY
01 GROUP-ITEM. 03 ITEM-A OCCURS 7 TIMES INDEXED BY IX. 05 NAME PIC X(20). 05 ADDRESS-1 PIC X(20). 05 ADDRESS-2 PIC X(20). 05 ADDRESS-3 PIC X(20). 05 CITY PIC X(20). 05 STATE PIC XX. 05 ZIP PIC 9(5). … PERFORM VARYING IX FROM 1 BY 1 UNTIL IX > 7 DISPLAY NAME(IX),CITY(IX),STATE(IX),SPACE,ZIP(IX) END-PERFORM.
About Index in TABLE ARRAY
- An index name (or just index) is an item that contains a value corresponding to an occurrence number of the table with which it is associated.
- Index(IX) is not necessarily stored in memory as an integer.
- A COBOL vendor may choose to implement it as a pointer to an element of the table, an offset from the beginning of the table, or whatever representation seems convenient for a particular COBOL implementation.
- The purpose of the index is to make accessing a table as efficient as possible.
In simple words,
- – Index is different form Index data item
- -Index name, it is not required to define explicitly in COBOL program
- -Index is associated with a Table on which it is mentioned
- -Index is not an identifier. So, index is not an Integer
- -Index names, you can use as variables in PERFORM,SET and SEARCH Statements
So for you got difference between Index-name and Index data item.
Index Data Item:
01 INDEX-1 USAGE is INDEX.
Benefits of using INDEX-DATA-ITEM:
- Can pass index data name to a sub-program(Note that it is not possible pass index name to a sub-program through a CALL statement)
- Cannot use Index data-item/data-name as Subscript
- Can use to store values of indexes to a file along with actual values.
Table definition with Multiple KEYS:
01 MAIL-INFO. 03 MAIL-LIST OCCURS 1000 TIMES ASCENDING KEY IS STATE, CITY. 05 NAME PIC X(20). 05 ADDRESS PIC X(20) OCCURS 3 TIMES. 05 CITY PIC X(20). 05 STATE PIC XX. 05 ZIP PIC 9(5).
Ascending/Descending Key is XYZ.
Interview Question on Keys:
- By specifying ascending/descending Key IS, does not mean that Table is in sorted order. This is only an expected order.
- COBOL real time example to write IF logic in Search statement
- COBOL arithmetic logic format you need
- CONTINUE vs SECTION in COBOL not the same why
- Write COBOL logic using COMPUTE for your project
- The Simplified COBOL Tutorial to refresh Quickly