Table Handling - COBOL

Tables of data are common components of business data processing problems. Although the repeating items that make up a table could be otherwise described as a series of separate data description entries all having the same level-number and all subordinate to the same group item, there are two reasons why this approach is not satisfactory.

First, from a documentation standpoint, the underlying homogeneity of the items would not be readily apparent; second, the problem of making available an individual element of such a table would be severe when there is a decision as to which element is to be made available at object time.

Tables of data items are defined by including the OCCURS clause in their data description entries. This clause specifies that the item is to be repeated as many times as stated. The item is considered to be a table element and its name and description apply to each repetition or occurrence. Since each occurrence of a table element does not have assigned to it a unique data-name, reference to a desired occurrence may be made only by specifying the identifier of the table element together with the occurrence number of desired table element. The occurrence number is specified by a subscript.

The number of occurrences of a table element may be specified to be fixed or variable depending on the value of another data item.

Table Definition

To define a one-dimensional table, use an OCCURS clause as part of the data description of the table element, but the OCCURS clause must not appear in the description of group items which contain the table element.


Defining a one-dimensional table within each occurrence of an element of another one-dimensional table gives rise to a two-dimensional table. To define a two- dimensional table, then, an OCCURS clause must appear in the data description of the element of the table, and in the description of only one group item which contains that table. In the description of a three-dimensional table, the OCCURS clause should appear in the data description of two nested group items which contain the element. The process of nesting table definitions may be continued to any depth, but the size of the outermost group increases geometrically with each dimension.

The following example shows how a three-dimensional table may be defined.


The data item named STATE-TABLE is a one-dimensional table.The data item named COUNTY-TABLE, which is subordinate to STATE-TABLE, is a two-dimensional table. The data item named CITY-TABLE, which is subordinate to COUNTY-TABLE, is a three-dimensional table.

Example defines 230,101 data items having a total size of 680,100 characters.See Table.

Example Definitions

Example Definitions

References to Table Items

Whenever the user refers to a table element or a condition-name associated with a table element, the reference must indicate which occurrence of the element is intended. For access to a one-dimensional table, the occurrence number of the desired element provides complete information. For access to tables of more than one dimension, an occurrence number must be supplied for each dimension of the table. In Example 2 then, a reference to the fourth STATE-CODE would be complete, whereas a reference to the fourth COUNTY-CODE would not.To refer to COUNTY-CODE, which is an element of a two-dimensional table, the user must refer to, for example, the fourth COUNTY-CODE within the sixth STATE-TABLE.

Occurrence numbers are specified by appending one or more subscripts to the data-name.

The subscript can be represented by an integer, a data-name that references an integer numeric elementary item, or an index-name associated with the table. A data-name or index-name may be followed by either the operator + or the operator – and an integer, which is used as an increment or decrement, respectively.It is permissible to mix integers, data-names and index-names.

The subscripts, enclosed in parentheses, are written immediately following any qualification for the name of the table element. The number of subscripts in such a reference must equal the number of dimensions in the table whose element is being referenced. That is, there must be a subscript for each OCCURS clause in the hierarchy containing the data-name including the data-name itself.

When more than one subscript is required, they are written in the order of successively less inclusive dimensions of the data organization.If a multidimensional table is thought of as a series of nested tables and the most inclusive or outermost table in the nest is considered to be the major table with the innermost or least inclusive table being the minor table, the subscripts are written from left to right in the order major, intermediate and minor.

A reference to an item must not be subscripted if the item is not a table element or an item or condition-name within a table element.

The lowest permissible occurrence number is 1.The highest permissible occurrence number in any particular case is the maximum number of occurrences of the item as specified in the OCCURS clause.

When an integer or data-name is used to represent a subscript, it may be used to reference items within different tables. These tables need not have elements of the same size. The same integer or data-name may appear as the only subscript with one item and as one of two or more subscripts with another item.

In order to facilitate such operations as table searching and manipulating specific items, a technique called indexing is available.To use this technique, the programmer assigns one or more index-names to 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 with which the index-name is associated.

The INDEXED BY phrase, by which the index-name is identified and associated with its table, is an optional part of the OCCURS clause.There is no separate entry to describe the index associated with index-name since its definition is completely hardware oriented. At object time, the contents of the index correspond to an occurrence number for that specific dimension of the table with which the index is associated. The initial value of an index at object time is undefined, and the index must be initialized before use. The initial value of an index is assigned with the PERFORM statement with the VARYING phrase, the SEARCH statement with the ALL phrase, or the SET statement.

The use of an integer or data-name as a subscript referencing 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 with which it is associated through 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 or binary searches. It is used to search a table for a table element that satisfies a specific condition and to adjust the value of the associated index to indicate that table element.

Relative indexing is an additional option for making references to a table element or to an item within a table element. When the name of a table element is followed by a subscript of the form (index-name + or – integer), the occurrence number required to complete the reference is the same as if index-name were set up or down by integer using the SET statement before the reference. The use of relative indexing does not cause the object program to alter the value of the index.

The value of an index can be made accessible to an object program by storing the value in an index data item. Index data items are described in the program by a data description entry containing a USAGE IS INDEX clause. The index value is moved to the index data item by the execution of a SET statement.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

COBOL Topics