beautypg.com

Compaq COBOL AAQ2G1FTK User Manual

Page 138

background image

Handling Tables
4.3 Accessing Table Elements

4.3.6 Index Data Items

Often a program requires that the value of an index be stored outside of that
item. Compaq COBOL provides the index data item to fulfill this requirement.

Index data items are stored as longword COMP items and must be declared with
a USAGE IS INDEX phrase in the item description. Index data items can be
explicitly modified only with the SET statement.

4.3.7 Assigning Index Values Using the SET Statement

You can use the SET statement to assign values to indexes associated with tables
to reference particular table elements. The following sections discuss the two
relevant SET statement formats. (All six SET statement formats are shown in
the Compaq COBOL Reference Manual.)

4.3.7.1 Assigning an Integer Index Value with a SET Statement

When you use the SET statement, the index is set to the value you specify. The
most straightforward use of the SET statement is to set an index name to an
integer literal value. This example assigns a value of 5 to IND-5:

SET IND-5 TO 5.

You can also set an index name to an integer data item. For example:

SET INDEX-A TO COUNT-1.

More than one index can be set with a single SET statement. For example:

SET TAB1-IND TAB2-IND TO 15.

Table indexes specified in INDEXED BY phrases can be displayed by using the
WITH CONVERSION option with the DISPLAY statement. Also, you can display,
move, and manipulate the value of the table index with an index data item. You
do this by setting an index data item to the present value of an index. You can,
for example, set an index data item and then display its value as shown in the
following example:

SET INDEX-ITEM TO TAB-IND.

.
.
.

DISPLAY INDEX-ITEM WITH CONVERSION.

4.3.7.2 Incrementing an Index Value with the SET Statement

You can use the SET statement with the UP BY/DOWN BY clause to
arithmetically alter the value of a index. A numeric literal is added to (UP
BY) or subtracted from (DOWN BY) a table index. For example:

SET TABLE-INDEX UP BY 12.

SET TABLE-INDEX DOWN BY 5.

4.3.8 Identifying Table Elements Using the SEARCH Statement

The SEARCH statement is used to search a table for an element that satisfies
a known condition. The statement provides for sequential and binary searches,
which are described in the following sections.

4–16 Handling Tables