HP 3000 Manuals

Tables [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation


HP COBOL II/XL Reference Manual

Tables 

Quite frequently in business applications, data is arranged in the form
of tables.  This is because of the logical arrangement of data and
because it is easier to both describe and select elements of a table than
it is to write all the components of the table as one record.

Tables composed of contiguous data items are defined in COBOL by using
the OCCURS clause in a record description entry.  The OCCURS clause
states how many elements there are in a table, gives these elements a
common name, tells whether the elements are arranged in ascending or
descending order, and whether to use subscripting or indexing to access
elements of the table.

You must use subscript or index names to access table elements because
they share the same name.

Defining a Table 

In HP COBOL II, you can define a table containing up to  seven 
dimensions.  This is accomplished by using the OCCURS clause once for
each dimension within different level numbers (other than 01) of the
description.  In ANSI COBOL'74, the maximum number of dimensions is
three.

To define a one dimensional table, use an OCCURS clause as part of the
data description for the table itself.  If you do not use the OCCURS
clause as part of the first level description following the table name,
the elements described before the OCCURS clause are not part of the
table.[REV BEG] For example, in the program fragment below, TABLE1-HEADER
is not a table element, whereas ELEMENT, TIME-ADJUSTER, and DATE-ADJUSTER
are table elements.[REV END]

     01 TABLE-1.
        02 TABLE1-HEADER                PIC X(20) VALUE "TABLE ONE".
        02 ELEMENT OCCURS 100 TIMES.
           03 TIME-ADJUSTER             PIC X(10).
           03 DATE-ADJUSTER             PIC X(10).

To define a two dimensional table, you must build it from a one
dimensional table.  That is, to define a two dimensional table, you must
use an OCCURS clause twice, once in an element of the first table, and a
second time in the description of a group item containing that table
element.  For example:

     01 SHOW-TABLE.
        11 FIRST-DIM OCCURS 10 TIMES.
           22 DIM1-HEAD               PIC X(20)
           22 SECOND-DIM OCCURS 10 TIMES.
              25 TWODIM-ELEMENTS.
                 30 MORE-ONE          PIC X(10).
                 30 MORE-TWO          PIC X(07).
                 30 MORE-THREE        PIC X(16).

The table element, SECOND-DIM, of the first table dimension (FIRST-DIM)
uses the OCCURS clause to define a second dimension of SHOW-TABLE, while
the group item FIRST-DIM defines the first dimension.

Defining a three dimensional table is analogous to defining a two
dimensional table.  Simply extend the table elements of the two
dimensional table to include an element that uses the OCCURS clause.  For
example:

     01 SALES-ORGANIZATION-TABLE.
        11 REGION-TABLE  OCCURS 4 TIMES.
           22 SALES-REGION                 PIC X(05).
           22 STATE-TABLE OCCURS 13 TIMES.
              33 STATE                     PIC X(20).
              33 REP-TABLE OCCURS 4 TIMES.
                 34 REP-INFO.
                    35 REPRESENTATIVE      PIC X(20).
                    35 LOCATION-INFO       PIC X(60).

The table above, named SALES-ORGANIZATION-TABLE, has one dimension for
REGION-TABLE, a second dimension for STATE-TABLE, and a third dimension
for REP-TABLE.

SALES-ORGANIZATION-TABLE contains 472 data items.  There are four data
items for REGION-TABLE, 52 data items for STATE-TABLE (4 times 13), and
416 data items for REP-TABLE (52 times 4, twice, one for each element
name).

Referencing Table Items with Subscripting 

Elements in a table of like elements can be uniquely referenced through
subscripts.  A subscript is an integer that corresponds to a specific
element in the table.  You can only use subscripts for elements that have
not been assigned individual data names.  The lowest possible subscript
value is 1, which identifies the first element in the table.  The next
ascending values (2, 3, 4...)  point to the second, third, and fourth
elements, and so forth.  The highest possible value is specified by the
OCCURS clause of the DATA DIVISION.

The table is identified by a table element data name or referenced by a
condition name.  Individual elements in the table are identified by one,
two, three,  or up to seven subscripts.  The subscript, or set of
subscripts, is enclosed in a pair of balanced parentheses following any
qualification for the table element data name.  (Any data name written in
this format is called a subscripted data name.)  When two or more
subscripts are used, they are written in order of successively decreasing
inclusiveness.  Subscripted data names have the following format:

[]
The subscript can be represented by[REV BEG] the reserved word ALL,[REV END] a numeric literal, a data name, or an index name. [REV BEG] The subscript ALL can be used only when the subscripted identifier is a parameter to a COBOL function. ALL cannot be used with condition-name-1. ALL specifies that each table element associated with that subscript position is a parameter to the function. See Chapter 10 , "COBOL Functions", for more information on calling COBOL functions.[REV END] Literals and data names must represent an integer, optionally preceded by a plus (+) sign. If a data name is used, it must specify an elementary item; the data name can be qualified but cannot itself be subscripted. The plus or minus sign must be preceded and followed by a space. An index is a special register containing a binary value that corresponds to an occurrence number of an element of the table to which it is associated. The implementation of an index is machine dependent for efficiency. The index is defined for the table and assigned an index name through the INDEXED BY phrase in the table definition in the DATA DIVISION. In the PROCEDURE DIVISION, you use the index name to reference the index. The index name must correspond to a data description entry in the hierarchy of the table being referenced that contains an INDEXED BY phrase specifying that index name. Before you can use the index as a table reference, however, you must assign the index an initial value. You do this by using the SET, SEARCH ALL, or PERFORM statement. [REV BEG] Indices, data-names, integers, and the word ALL can be combined to reference a multidimensional table. [REV END] Two subscripting techniques are available: * Direct subscripting, where the element desired is specified by the contents of the subscript. * Relative subscripting, where the element desired is specified by the contents of the subscript plus or minus a specific value. Direct subscripting is specified by using a subscript after the table element data name. For example: DATA-1 (INDEX-A) Relative subscripting is specified by using the subscript, followed by a plus or minus sign, followed by an unsigned integer specified as a numeric literal, all enclosed in balanced parentheses and following the table element data name. The compiler determines the sequential location of the element in the table by incrementing (for the plus sign) or decrementing (for the minus sign) the value in the index or data item by the value of the literal. The following illustrates direct subscripted data items. DAY (1) DATE (1 2) BENCHMARK (ALPHA BETA GAMMA) CANDY (20 FILEZ-01 +3) The following illustrates relative subscripting. TABLE-1 (BETA + 1) TABLE-2 (THETA - 1 MU) ARRAY-3 (STORE-1 + 1 STORE-2 + 1 STORE-3 + 1) In the first of the above examples, if TABLE-1 is the table name and the value of BETA is 15, the program accesses the 16th element. The second example demonstrates that both direct and relative subscription are permitted in the same subscripted data name. Subscripting uses an occurrence number (that is, the number of where in a particular dimension an element occurs) for each dimension of a table. To illustrate, using the three dimensional table SALES-ORGANIZATION-TABLE described under "Defining a Table", the following refers to the third occurrence of REPRESENTATIVE in the first state of the fourth sales region: REPRESENTATIVE (4, 1, 3) Thus, if the fourth sales region is the western sales region, and the first state in that region is California, then the name of the third representative for that state is accessed by the above subscripted reference. Of course, if you wish only to access a state entry, you can do so by using only two subscripts. For example, the following references the 12th state in the fourth sales region: STATE(4, 12) Note that data names could as easily have been used to perform all or just part of the subscripting above. Generally, these data items are defined in working storage, and have no restrictions on them except that they cannot be index data names. Referencing Table Items with Indexing Indexing requires more coding in the OCCURS clause, since you must specify at least one name to be used for the indexing. To illustrate, the SALES-ORGANIZATION-TABLE has been modified for indexing: 01 SALES-ORGANIZATION-TABLE. 11 REGION-TABLE OCCURS 4 TIMES. 22 SALES-REGION PIC X(05). 22 STATE-TABLE OCCURS 13 TIMES. 33 STATE PIC X(20). 33 REP-TABLE OCCURS 4 TIMES INDEXED BY RPINDX. 34 REP-INFO. 35 REPRESENTATIVE PIC X(20). 35 LOCATION-INFO PIC X(60). Once the index names have been defined, you must use the SET statement of the PROCEDURE DIVISION to initialize the index names to a value within the range of from 1 to the highest occurrence number associated with the dimension in which the index name was defined. See "OCCURS Clause" in Chapter 7 for more details. Once an index name has been set, you can use it to access table elements. Assuming that RPINDX has been set to 2, the following example accesses the information about the second representative in the first state of the fourth sales region: REP-INFO(4, 1, RPINDX) Referring to the use of this same data base in the subscript example above, note that this accesses the information about the second sales representative in California. You can use index names in conjunction with the SEARCH statement of the PROCEDURE DIVISION to search for occurrences of table items within a given table. For information and restrictions on searching tables, refer to "SEARCH Statement" in Chapter 9 . Condition Names Condition names made unique through qualification, indexing, or subscripting have the same overall syntax for identifiers as the two formats above. In these formats, however, the user-defined word condition-name-1 replaces data-name-1. The restrictions that apply to the combined use of qualification, subscripting, and indexing of identifiers also apply to condition names. In addition, these further restrictions apply: 1. If a condition name is made unique through qualification, you can use either the hierarchy of names associated with the related conditional variable or the conditional variable itself as the first qualifier. 2. If references to a conditional variable require indexing or subscripting, you must use the same indexing or subscripting for references to any condition name associated with that variable.
NOTE In the format descriptions appearing throughout this manual, condition-name refers to a condition name that can be qualified, indexed, or subscripted as necessary.
[REV BEG]


MPE/iX 5.0 Documentation