Subscripting Using Index-Names (Indexing)

Indexing allows such operations as table searching and manipulating specific items. To use indexing you associate one or more index-names with an item whose data description entry contains an OCCURS clause. An index associated with an index-name acts as a subscript, and its value corresponds to an occurrence number for the item to which the index-name is associated.

The INDEXED BY phrase, which identifies the index-name associated with its table, is an optional part of the OCCURS clause. There is no separate entry to describe the index-name since its definition is completely system dependent. Index-names may be seen as compiler generated registers for the use of this program only. They are not data, or part of the data hierarchy, and must be unique in a COBOL program.

Each index name must follow the rules for formation of a user-defined word.

Each index-name refers to a compiler-generated register or storage area.

The initial value of an index at object time is undefined, and the index must be initialized before it is used as a subscript. The initial value of an index is assigned with:
  • The PERFORM statement with the VARYING phrase, or
  • The SEARCH statement with the ALL phrase, or
  • The SET statement.

An index-name can only be referenced by a PERFORM, SET, or SEARCH statement, as a parameter in the USING phrase in a CALL statement, or in a relational condition comparison.

The use of an integer or data-name as a subscript referencing a table element or an item within a table element does not cause the alteration of any index associated with that table.

An index-name can be used to reference only the table to which it is associated by the INDEXED BY phrase.

Data that is arranged in the form of a table is often searched. The SEARCH statement provides facilities for producing serial and non-serial (for example, binary) searches. It is used to search for a table element that satisfies a specific condition and to adjust the value of the associated index to indicate that table element.

To be valid during execution, an index value must correspond to a table element occurrence of neither less than one, nor greater than the highest permissible occurrence number.

Further information on index-names is given in the description of the INDEXED BY phrase of the OCCURS clause. See INDEXED BY Phrase.