Module 9: Array processing and Table handling
ARRAY INDEX and INDEXING
- Array INDEX is one of the good alternative to subscripting
- An INDEX denotes displacement of the element from the beginning of array
- An INDEX is data item which is associated with an array or particular dimension of an array through the use of INDEXED BY phrase of OCCURS clause
01 COMPANY-TABLE. 05 DEPARTMENT OCCURS 3 TIMES INDEXED BY D1. 10 EMPLOYEE OCCURS 6 TIMES INDEXED BY E1. 15 SALES PIC 9(4) TIMES OCCRUS 4 TIMES INDEXED BY S1.
- In above example, D1, E1 and S1 are index names.
- These index names need not be defined in DATA DIVISION
- Array element can be referenced as SALES(D1,E1,S1)
- General format of using INDEXED BY phrase with OCCURS clause:-
OCCURS integer TIMES [INDEXED BY index-name-1 [, index-name-2]…]
- Some points to be considered while using INDEXED BY phrase:-
- If INDEXED BY used for any one level of array, then INDEXED BY must be used for all levels
- INDEX item cannot be used in combination with subscript data item
- INDEX defined for one array cannot be used for another array. The index name should be unique
- Since INDEX items are coded with INDEXED BY phrase, it should not appear in DATA DIVISION
- INDEX items can be manipulated using SET, SEARCH and PERFORM statements
- Use of INDEX with plus or minus to any integer is valid. For example, SALES(D1+2,E1-1,S2) is valid
- More than one index item can be coded for each level of array
What other kind of INDEX exist?
- There is one more kind of INDEX items exist which are declared in DATA DIVISION by specifying USAGE IS INDEX clause
- And INDEX declared with ‘USAGE IS INDEX’ clause must not have PIC clause
77 INDEX-SALES USAGE IS INDEX.This defines Index ‘INDEX-SALES’.
- Such kind of data items are called as Index Data item
- This kind of index data item can be used to temporarily store the index values.
- With help of SET verb, the actual index value can be transferred to the index data item(and vice-versa) without conversion (to occurrence number). SET verb is explained in next section.
- When the clause is specified for a group item, it applies to all elementary items contained in it. However, the group itself is not an index data item
- For example,
In DATA DIVISION,01 TEMP-INDEX. 02 A-INDEX USAGE IS INDEX. 01 PERM-INDEX. 02 INDEX-VALUE PIC S9(6) COMP.Now, In PROCEDURE DIVISIONMOVE A-INDEX TO PERM-INDEX MOVE A-INDEX TO INDEX-VALUEare both invalid statements. However, the following statement is validMOVE TEMP-INDEX TO PERM-INDEX