TABLE Handling in cobol

Also known as an array in other languages like 'C', a table is a group
of data associated with a single item name.

To identify pieces data (elements) within that table the item is given
a subscript number which follows the name.

W-NAME (elements) Subscript
-----------------------------------------------------
Smith 1
Jones 2
MacDoon 3
Walker 4
O'Leary 5
----------------------------------------------------
So, DISPLAY W-NAME (2) will give "Jones".

A 2-dimensional table uses two subscripts:
So element (2, 4) will contain "1.1".

To define the W-NAME (1-dimensional) table in the data division:

01 W-NAME PIC X(10) OCCURS 5 TIMES.


The word TIMES is optional. Also, the PIC clause can also be written
after the OCCURS ... clause.

To define the SALES-TABLE (2-dimensional) table, just add another
level to the group. Hence:

Tables

01 SALES-TABLE.
03 BRANCH-NO OCCURS 4.
05 MONTHLY-SALES OCCURS 4 PIC 9V9.

Notice how only the top level 05 contains the PIC clause. Level 01
describes a whole table made up of 4 items (level 03) containing 4
elements (level 05).

Table can be multi-dimensional, but always the last level will be the
identifier name that is associated with the subscripts and will have
the PIC clause.

For the use of tables, see the League Table Program in the Sample Code section.

Finally, don't try to refer to a table element that is of a greater
value than that defined in the data division, i.e. W-NAME (6) will
cause a runtime error and terminate the program. It should be obvious
that a subscript should be a numeric literal or an identifier that is
numeric.

The use of identifiers as subscripts is where tables are of most use,
i.e. MONTHLY-SALES (INPUT-BRANCH, INPUT-MONTH).

No comments:

Post a Comment