The COBOL occurs, Usage is Index, Indexed By clause are tricky. And these are confused by many people. In this post, I have shared three key points:
- Indexed BY
- Usage is Index
1). COBOL Occurs
COBOL programmers confused very much about how to use the OCCURS clause with the KEY phrase correctly. The syntax for OCCURS clause with Indexed BY
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
2). COBOL Index
Indexed is different from the index data item (Usage is an index). This post gives you a complete idea.
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 efficiently as possible.
In simple words:
- The index is different from the Index data item
- The index is not required to define explicitly in the COBOL program
- The index is associated with a Table on which it is mentioned
- The index is not an identifier. So, the index is not an Integer
- Index names you can use as variables in PERFORM, SET, and SEARCH Statements
You are now good with what is an index. Now, I will share the key points on what is an index data item.
3). Usage is Index Data Item:
01 INDEX-1 USAGE is INDEX. or 77 INDEX-1 USAGE is INDEX.
Benefits of Usage is Index Data item
- Number one benefit is, you can pass the Usage-is-index data item to a SUBPROGRAM (the indexed-value you cannot pass to the sub-program)
- The other benefit is, you can write the usage-is-index data items to a file.
- How we use Index the same way you can use usage is an index data item.
How to Display Usage is Index data item
4). How to Define Key
Ascending/Descending Key is XYZ.
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).
Interview Question on Keys:
- By specifying ascending/descending Key IS, it doesn’t mean that the Table is in sorted order. This is only an expected order.
Comments are closed.