Friday, 12 December 2008

INDEXED TABLES AND INDEXING:

COBOL has an alternative to subscripting. This is known as indexing. An index is a data item which is associated with a table or a particular dimension of a table through the use of INDEXED BY phrase of an OCCURS clause. The following example illustrated table description with the INDEXED BY Phrase

01 ENROLL-TABLE.

02 FACULTY OCCURS 3 TIMES INDEXED BY F1.

03 DEPARTMENT OCCURS 6 TIMES INDEXED BY D1.

04 YEAR PIC 9(4) OCCURS 5 TIMES INDEXED BY Y1.

Here, F1, D1 and Y1 are index names associated with this table. These need not be defined separately in the DATA DIVISION.

A subscript is any integer data item and its value denotes the position or OCCURRENCE number of the element within a table.

The exact representation of an index is system dependent, generally an index denotes a displacement of the element from the beginning of the table. When we write

04 YEAR PIC 9(4) OCCURS 6 TIMES INDEXED BY Y1.

The displacement for the first element is zero, that for the second element is 4, (note that the size is zero, that for the second element is 4), that for the third element is 8 and so on.

The format of the OCCURS clause with INDEXED By phrase is

OCCURS integer TIMES

[INDEXED BY index-name-1[,index-name-2]…….]


The following rules apply when INDEXED BY phrase is used.

  • If indexing is done for any one level of a table, then indexing must be used for all levels.
  • Index names cannot be used in combination with subscripts.
  • Indexes are valid only for the tables where they have been specified.
  • The index names must be unique. The same index name must be used for different levels of a table.
  • The indexes must not appear anywhere in the DATA DIVISION except in the INDEXED by phrase of the OCCURS clause.
  • Indexes can be manipulated only by the SET, SEARCH and PERFORM statements.
  • An index can be coded plus or minus an integer literal for the relative addressing of the table elements. For example, YEAR (F1+1, D1 - 2,Y1 – 1) is valid.
There can be more than one index for each level.

No comments:

Post a Comment