P. 1
COBOL - Programming Guide

COBOL - Programming Guide

|Views: 968|Likes:
Publicado pormukesh

More info:

Published by: mukesh on Jul 01, 2011
Direitos Autorais:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
See more
See less


You can also refer to table elements by using an index.An index is a displacement
from the start of the table, based on the length of the table element.Use the index
in SET, SEARCH, SEARCH ALL, PERFORM VARYING, or relational condition
statements.An index-name must be initialized through a SET, PERFORM
VARYING, or SEARCH ALL statement before it is used in a table reference.

The compiler determines the index of an entry based on the following formula:

I = L*(S-1)



is the index value


is the length of a table entry


is the subscript (occurrence number) of an entry

You define the index-name for a table in the INDEXED BY clause of the OCCURS
clause in the table definition.

To be valid during execution, an index value must correspond to a table element
occurrence of not less than 1 nor greater than the highest permissible occurrence
number.This restriction applies to both direct and relative indexing.

In direct indexing, the index-name is in the form of a displacement.The value
contained in the index is then calculated as the occurrence number minus 1, multi-
plied by the length of the individual table entry.For example:

5Table-Item occurs 1 indexed by Inx-A PIC X(8).

Chapter 7.Handling Tables (Arrays)95

Coding Your Program

For the fifth occurrence of Table-Item, the binary value contained inInx-A is (5 - 1)
* 8 = 32.

In relative indexing, the index-name is followed by a space, followed by a + or a -,
followed by another space, followed by an unsigned numeric literal.The literal is
considered to be an occurrence number, and is converted to an index value before
being added to or subtracted from the index-name.For example, if you specify
indexing forSample-Table-Three as follows:

5 Table-Depth occurs 3 times indexed by Inx-A.
1 Table-Column occurs 4 times indexed by Inx-B.
15 Table-Row occurs 8 times indexed by Inx-C

PIC X(8).

then a relative indexing reference to

Table-Row (Inx-A + 1, Inx-B + 2, Inx-C - 1)

causes the following calculation of the displacement:

(contents of Inx-A) + (256 ᑍ 1)
+ (contents of Inx-B) + (64 ᑍ 2)
+ (contents of Inx-C) - (8 ᑍ 1)

This means:

Each occurrence of Table-Depth is 256 characters in length
Each occurrence of Table-Column is 64 characters in length
Each occurrence of Table-Row is 8 characters in length

One or more index references (direct or relative) may be specified together with
literal subscripts.

To compare two different occurrences of a table element, use a direct indexing ref-
erence together with a relative indexing reference, or use subscripting, which is
easier to read in your code.

An index can be shared among different tables.That is, you can use the index
defined with one table to index another table if both table descriptions are identical.
To be identical, the tables must have the same number of occurrences, as well as
occurrences of the same length.

You can use index data items to store index values.You can use the SET state-
ment to assign to an index the value that you stored in the index data item.The
index data item holds the physical displacement value.You define an index data
item with the USAGE IS INDEX clause.

For example, when you read records to load a variable-length table, you can store
the index value of the last record in a data item defined as USAGE IS INDEX.
Then, when you use the table index to look through or process the variable-length
table, you can test for the end of the table by comparing the current index value
with the index value of the last record you stored in the index data item.

Because you are comparing a physical displacement, you can use index data items
only in SEARCH and SET statements or for comparisons with indexes or other
index data items.You cannot use index data items as subscripts or indexes.

96COBOL/VSE Programming Guide

Coding Your Program

Subscripting and Indexing Restrictions

1.A data-name must not be subscripted or indexed when it is being used as a
subscript or qualifier.

2.An index can be modified only by a PERFORM, SEARCH, or SET statement.

3.When a literal is used in a subscript, it must be a positive or unsigned integer.

4.When a literal is used in relative indexing or relative subscripting, it must be an
unsigned integer.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->