COBOL TABLES
1. Sequence of consecutive storage locations having a common name.
2. Each location is called an element
3. Each element is referred to by table name and subscript enclosed in parenthesis
4. Subscripts can be constants or variables
USE of TABLES
Must define in working storage
The OCCURS clause is used to define a table
General Format
OCCURS integer TIMES
Where integer = # of elements in Table
Cannot be used with a 01 Level item!
Occurs with elementary items
01 Business-Table.
05 Name
OCCURS 50 TIMES
05 Class
OCCURS 50 TIMES
05 Hours OCCURS 50 TIMES
PIC X(20).
PIC X.
PIC 9(3).
Table Contains 150 elements
Table Contains 1200 Characters
For this Table, Subscripts may only be used with the
elementary items, thus:
Name(10) is Valid
Class(5) is Valid
Hours(15) is Valid
Business-Table(2) is not Valid!
Occurs with Group Items
01 Business-Table.
05 Student-Data OCCURS 50 TIMES.
10 Name PIC X(20).
10 Class PIC X.
10 Hours PIC 9(3).
Table Contains 150 elements
Table Contains 1200 Characters
For this Table, Subscripts may be used with the
group items and the elementary items, thus:
Student-Data(30) is valid
Name(10) is Valid
Class(5) is Valid
Hours(15) is Valid
Business-Table(2) is not Valid!
Perform Varying
Used to step through a table to initialize values.
General Format:
PERFORM paragraph VARYING dataname
FROM initial-value BY increment-value
UNTIL condition.
Initial value and increment-value may be either numeric
datanames or numeric constants.
Example
PERFORM PARA-1 VARYING INDX FROM 1 BY 1
UNTIL INDX > 10.
STEPS:
1.
INDX is initialized to 1.
2.
If condition (INDX > 10) is NOT met, PARA-1 is executed.
3.
After PARA-1 is executed, 1 is added to INDX.
4.
Return to step 2 and check the condition.
Loading Tables
Execution Time Tables - data loaded when program is run.
Compile Time Tables - data loaded when program is compiled.
Execution Time Tables
Used when data is expected to change often.
1. Define Table in Working-Storage:
01 Business-Table.
05 Student-Data OCCURS 50 TIMES.
10 Name PIC X(20).
10 Class PIC X.
10 Hours PIC 9(3).
01 Num-In
PIC 99 VALUE ZERO.
02 ERROR-FLAG PIC X(3) VALUE NO.
2. Initialize Table in Procedure Division:
PERFORM Init-Table VARYING Num-In
FROM 1 BY 1 UNTIL Num-In > 50.
.
.
.
Init-Table.
MOVE ZEROS TO Hours(Num-In).
MOVE SPACES TO Name(Num-In)
Class(Num-In).
3. Load data into the table:
MOVE ZERO TO Num-In.
READ Student-File INTO Student-Detail
AT END MOVE YES to EOF-FLAG.
PERFORM 100-Load-Table Until EOF-FLAG = YES
OR Error-Flag=YES.
100-Load-Table.
ADD 1 to Num-In.
IF Num-In > 50 MOVE YES TO Error-Flag
ELSE MOVE Name-In to Name(Num-In)
MOVE Class-In to Class(Num-In)
MOVE Hours-In to Hours(Num-In)
READ Student-File INTO Student-Detail
AT END MOVE YES to EOF-FLAG.
Compile Time Tables
Used for More Permanent Storage
Days of Week, Month Names, et cetera
In working Storage, define data and table structure.
Values are stored during compilation of program
Uses Redefines clause
REDEFINES
General Format:
Level-Number
dataname-1 REDEFINES dataname-2
The level-number on the REDEFINES entry must be the same as the level-number on
dataname-2, which is being redefined.
Define & Load Table in Working-Storage
01 Class-Names.
05 Filler
05 Filler
05 Filler
05 Filler
Pic X(9)
Pic X(9)
Pic X(9)
Pic X(9)
Value Freshman.
Value Sophomore.
Value Junior.
Value Senior.
01 Class-Table REDEFINES Class-Names.
05 Class OCCURS 4 TIMES PIC X(9).
Class-Names and Class-Table are 2 different names
for the same 36 (9 * 4) characters in storage.
FreshmanbSophomoreJuniorbbbSeniorbbb
CLASS(3) = Junior
Redefines Rules
1. Level-Numbers on the REDEFINES entry must be the same as the level-number
on the dataname that is being redefined.
2. A dataname with a lower level-number may NOT occur between the
REDEFINES entry and the dataname being redefined. However, datanames with
higher level-numbers are acceptable here.
3. The length of the item being REDEFINED must be exactly the same length as the
item it is redefining.
Indexed by Clause
General Format:
OCCURS integer TIMES INDEXED BY index-name
Example:
01 Class-Names.
05 Filler
05 Filler
05 Filler
05 Filler
Pic X(9)
Pic X(9)
Pic X(9)
Pic X(9)
Value Freshman.
Value Sophomore.
Value Junior.
Value Senior.
01 Class-Table REDEFINES Class-Names.
05 Class OCCURS 4 TIMES
INDEXED BY CLASS-INDEX PIC X(9).
Table Value
Occurrence (subscript)
Displacement (Index)
Freshman
Sophomore
Junior
18
Senior
27
SUBSCRIPTS vs. INDEXES
When an index-name is specified, the compiler automatically provides for the
index. It is not necessary to define a separate data item in Working-Storage as is
done for a variable subscript.
Indexes are displacement values with subscript represent occurrence numbers
Since Index values are displacement values, an index cannot be initialized with a
MOVE statement like a subscript can. Similiarly, we cannot ADD to or
SUBTRACT from an index. Instead, the SET verb must be used.
SET VERB
Used to initialize or change the value of a Table index
To Initialize a table index:
SET index-name TO integer (or numeric variable).
Examples (in procedure division)
SET Class-Index to 1.
SET Class-Index to Class-In.
To INCREMENT OR DECREMENT
SET index-name UP BY (or DOWN BY)
Integer (or numeric variable).
Examples (In Procedure Division)
SET Class-Index UP BY 1.
SET Class-Index Down By 2.
Reference:
www.tutorialspoint.com/cobol/cobol_table_processing.