Cobol Array Processing and Tables
Cobol Array Processing and Tables
Handling
OBJECTIVES
CONTENTS
CONTENTS
Defining a Table
CONTENTS
CONTENTS
CONTENTS
An Introduction to
Single-Level OCCURS Clauses
With a table, we use the contents of some input filed to "look up" the
required data in the table.
01 TEMP-REC.
05 TEMPERATURE OCCURS 24 TIMES PIC X9(3)
Defining a Subscript
SUMMARY OF OCCURS
AND SUBSCRIPTS
1. An OCCURS clause is defined in the DATA
DIVISION to indicate the repeated occurrence of
items in an array that have the same format.
2. A subscript is used in the PROCEDURE DIVISION to
indicate which specific item within the array we
wish to access.
QUESTIONS?
SELF-TEST
SELF-TEST
1. An OCCURS clause could be used in place of
defining each AMT field separately because
____________________________________ .
SELF-TEST
2. (T or F) Suppose AMT2 and AMT4 had PIC 9(3). An
OCCURS clause could not be used to define all
the AMT fields.
SOLUTION: T
SELF-TEST
3. Recode the fields within IN-REC using an OCCURS
clause.
SOLUTION:
01 IN-REC.
05 AMT OCCURS 5 TIMES
PIC 9(5).
SELF-TEST
4. To access any of the five items defined with the
OCCURS clause, we must use a ____ in the
PROCEDURE DIVISION.
SOLUTION: subscript
SELF-TEST
5. Code a routine to determine the total of all five AMT fields.
Assume that a field called SUB has been defined in WORKINGSTORAGE to serve as a subscript.
SOLUTION: COBOL 85 (With an In-line PERFORM)
MOVE ZEROS TO TOTAL
PERFORM VARYING SUB FROM 1 BY 1 UNTIL SUB > 5
ADD AMT (SUB) TO TOTAL
END-PERFORM
...
Levels 02--49
That is, the OCCURS is not valid for the 01 level since
it must be used for defining fields, not records.
Defining a Table
statement-2} {NEXT
[END-SEARCH]*
*COBOL 85 only. If END-SEARCH is used, NEXT SENTENCE must be
replaced with CONTINUE unless your COBOL 85 compiler has an
enhancement that permits it.
Without it, the ``no match'' condition would simply cause the
program to continue with the next sentence; thus producing
incorrect results or even a program interrupt.
DIFFERENCES BETWEEN
SUBSCRIPTS AND INDEXES
Subscript represents an occurrence of an array or table element.
ADD 1 TO SUB
DIFFERENCES BETWEEN
SUBSCRIPTS AND INDEXES
Index represents a displacement from the first address in the array or
table.
SET X1 UP BY 1
Definition of a Serial
Search
1. The first entry in the table is searched.
2. If the condition is met, the table look-up is completed.
2. Table entries are organized so that the first values are the ones
encountered most frequently;
in this way, access time is minimized because you are apt to end the
search after the first few comparisons.
Definition of a Binary
Search
such a case, we compare CUST-NO-IN to TCUSTOMER-NO (37), the middle table argument of
the second half of the table (rounding down), and
continue our comparison in this way.
Definition of a Binary
Search
3. If CUST-NO-IN < T-CUSTOMER-NO (25), we compare CUST-NO-IN to
T-CUSTOMER-NO (12);
that is, we divide the top half of the table into two segments and
continue our comparison.
4. The binary search is complete either (a) when a match has been
found, that is, CUST-NO-IN = T-CUSTOMER-NO (X1), or (b) when
the table has been completely searched.
5. The OCCURS item and its index, which define the table argument,
must appear to the left of the equal sign.
Can include any relational test with the WHEN clause (<, >, =, <=, >=)
or any compound conditional
Can only have a single = condition tested with the WHEN clause
MULTIPLE-LEVEL OCCURS
CLAUSE
Double-Level or
Two-Dimensional Array
UNTIL condition-1
[AFTER {identifier-5}{index-name-3}
FROM {identifier-6}{index-name-4} literal-3}
BY {identifier-7} {literal-4} UNTIL condition-2]
[END-PERFORM]*