[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
User Manual


Previous Contents Index

4.2 Initializing Values of Table Elements

You can initialize a table that contains only DISPLAY items to any desired value in either of the following ways:

  • You can specify a VALUE clause in the record level preceding the record description of the item containing the OCCURS clause.
  • You can specify a VALUE clause in a record subordinate to the OCCURS clause.

Example 4-11 and Figure 4-10 provide an example and memory map of a table initialized using the VALUE clause.

Example 4-11 Initializing Tables with the VALUE Clause

01 A-TABLE VALUE IS "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC".
    03 MONTH-GROUP PIC XXX USAGE DISPLAY
               OCCURS 12 TIMES.

Figure 4-10 Memory Map for Example on Initializing Tables with the VALUE Clause


If each entry in the table has the same value, you can initialize the table as shown in Example 4-12.

Example 4-12 Initializing a Table with the OCCURS Clause

01 A-TABLE.
   03 TABLE-LEG OCCURS 5 TIMES.
      05 FIRST-LEG   PIC X VALUE "A".
      05 SECOND-LEG  PIC S9(9) COMP VALUE 5.

In this example, there are five occurrences of each table element. Each element is initialized to the same value as follows:

  • FIRST-LEG occurs five times; each occurrence is initialized to A.
  • SECOND-LEG occurs five times; each occurrence is initialized to 5.

Often a table is too long to initialize using a single literal, or it contains numeric, alphanumeric, COMP, COMP-1, COMP-2, or COMP SYNC items that cannot be initialized. In these situations, you can initialize individual items by redefining the group level that precedes the level containing the OCCURS clause. Consider the sample table descriptions illustrated in Example 4-13 and Example 4-14. Each fill byte between ITEM1 and ITEM2 in Example 4-13 is initialized to X. Figure 4-11 shows how this is mapped into memory.

Example 4-13 Initializing Mixed Usage Items

01 A-RECORD-ALT.
   05 FILLER PIC XX VALUE "AX".
   05 FILLER PIC S99 COMP VALUE 1.
   05 FILLER PIC XX VALUE "BX".
   05 FILLER PIC S99 COMP VALUE 2.
   .
   .
   .
01 A-RECORD REDEFINES A-RECORD-ALT.
   03 A-GROUP OCCURS 26 TIMES.
      05 ITEM1 PIC X.
      05 ITEM2 PIC S99 COMP SYNC.

Figure 4-11 Memory Map for Example on Initializing Mixed Usage Items


As shown in Example 4-14 and in Figure 4-12, each FILLER item initializes three 10-byte table elements.

Example 4-14 Initializing Alphanumeric Items

01 A-RECORD-ALT.
    03 FILLER PIC X(30) VALUE IS
       "AAAAAAAAAABBBBBBBBBBCCCCCCCCCC".
    03 FILLER PIC X(30) VALUE IS
       "DDDDDDDDDDEEEEEEEEEEFFFFFFFFFF".
    .
    .
    .
01 A-RECORD REDEFINES A-RECORD-ALT.
    03 ITEM1 PIC X(10) OCCURS 26 TIMES.

Figure 4-12 Memory Map for Example on Initializing Alphanumeric Items


When redefining or initializing table elements, allow space for any fill bytes that may be added due to synchronization. You do not have to initialize fill bytes, but you can do so. If you initialize fill bytes to an uncommon value, you can use them as a debugging aid in situations where a Procedure Division statement refers to the record level preceding the OCCURS clause, or to another record redefining that level.

You can also initialize tables at run time. To initialize tables at run time, use the INITIALIZE statement. This statement allows you to initialize all occurrences of a table element to the same value. For more information about the INITIALIZE statement, refer to the HP COBOL Reference Manual.

Sometimes the length and format of table items are such that they are best initialized using Procedure Division statements such as a MOVE statement to send a value to the table.

4.3 Accessing Table Elements

Once tables have been created using the OCCURS clause, the program must have a method of accessing the individual elements of those tables. Subscripting and indexing are the two methods HP COBOL provides for accessing individual table elements. To refer to a particular element within a table, follow the name of that element with a subscript or index enclosed in parentheses. The following sections describe how to identify and access table elements using subscripts and indexes.

4.3.1 Subscripting

A subscript can be an integer literal, an arithmetic expression, a data name, or a subscripted data name that has an integer value. The integer value represents the desired element of the table. An integer value of 3, for example, refers to the third element of a table.

4.3.2 Subscripting with Literals

A literal subscript is an integer value, enclosed in parentheses, that represents the desired table element. In Example 4-15, the literal subscript (2) in the MOVE instruction moves the contents of the second element of A-TABLE to I-RECORD.

Example 4-15 Using a Literal Subscript to Access a Table

Table Description:

      01 A-TABLE.
         03 A-GROUP PIC X(5)
            OCCURS 10 TIMES.

Instruction:

         MOVE A-GROUP(2) TO I-RECORD.

If the table is multidimensional, follow the data name of the desired data item with a list of subscripts, one for each OCCURS clause to which the item is subordinate. The first subscript in the list applies to the first OCCURS clause to which that item is subordinate. This is the most inclusive level, and is represented by A-GROUP in Example 4-16. The second subscript applies to the next most inclusive level and is represented by ITEM3 in the example. Finally, the third subscript applies to the least inclusive level, represented by ITEM5. (Note that HP COBOL can have 48 subscripts that follow the pattern in Example 4-15.)

In Example 4-16, the subscripts (2,11,3) in the MOVE statements move the third occurrence of ITEM5 in the eleventh repetition of ITEM3 in the second repetition of A-GROUP to I-FIELD5. ITEM5(1,1,1) refers to the first occurrence of ITEM5 in the table, and ITEM5(5,20,4) refers to the last occurrence of ITEM5.

Example 4-16 Subscripting a Multidimensional Table

Table Description:

      01 A-TABLE.
         03 A-GROUP OCCURS 5 TIMES.
            05 ITEM1           PIC X.
            05 ITEM2           PIC 99 COMP OCCURS 20 TIMES.
            05 ITEM3 OCCURS 20 TIMES.
               07 ITEM4        PIC X.
               07 ITEM5        PIC XX OCCURS 4 TIMES.
      01 I-FIELD5              PIC XX.

Procedural Instruction:

            MOVE ITEM5(2, 11, 3) TO I-FIELD5.

Note

Because ITEM5 is not subordinate to ITEM2, an occurrence number for ITEM2 is not permitted in the subscript list (when referencing ITEM3, ITEM4, or ITEM5). The ninth occurrence of ITEM2 in the fifth occurrence of A-GROUP will be selected by ITEM2(5,9).

Table 4-1 shows the subscripting rules that apply to Example 4-16.

Table 4-1 Subscripting Rules for a Multidimensional Table
Name of Item Number of Subscripts
Required to Refer to
the Name Item
Size of Item in Bytes
(Each Occurrence)
A-TABLE NONE 1105
A-GROUP ONE 221
ITEM1 ONE 1
ITEM2 TWO 2
ITEM3 TWO 9
ITEM4 TWO 1
ITEM5 THREE 2


Previous Next Contents Index