Mainframe-COBOL Occurs, Indexed By,USAGE is Index (1 of 2)

Top 30 High Paying JobsI want to share very important points on COBOL index. First point is Index cannot passed into SUB-PROGRAM and why. Second, 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:

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:

  1. 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)
  2. Cannot use Index data-item/data-name as Subscript
  3. 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).

Defining Key:

Ascending/Descending Key is XYZ.

Interview Question,Occurs,Keys:

  • By specifying ascending/descending Key IS, does not mean that Table is in sorted order. This is only an expected order.

Next post I will add more points.Keep Reading.

Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.