Professional Documents
Culture Documents
Cobol Arrays
Cobol Arrays
Handling
CONTENTS:
SINGLE-LEVEL OCCURS Clauses
Processing data Stored in an ARRAY
Using an OCCURS Clause for Table Handling
Use the SEARCH statement for Table and
Array Processing
The SEARCH…VARYING Option for
processing PARALLEL Tables
The SEARCH ALL Statements
MULTIPLE LEVEL OCCURS Clause
SINGLE-LEVEL OCCURS
Clause
Why OCCURS Clauses are used?
1. Defining a series of input or output fields, each
with the same format.
2. Defining a series of totals in WORKING-
STORAGE to which amounts are added; after
all data is accumulated, the totals can be
printed.
3. Defining a table in WORKING-STORAGE to
be accessed by each input record. With table
we use the contents of some input field to
“look up” the required data in the table.
SINGLE-LEVEL OCCURS
Clause
Defining Fields with an OCCURS Clause:
01 TEMP-REC.
05 TEMPERATURE OCCURS 24 TIMES PIC S9(3).
500-ADD-RTN.
DISPLAY "ENTER TEMPERATURE ( ) >> " AT LINE SUB
COLUMN 1.
DISPLAY SUB AT LINE SUB COLUMN 20.
ACCEPT TEMPERATURE (SUB) AT LINE SUB COLUMN 28.
ADD TEMPERATURE (SUB) TO TOTAL-TEMP.
PROCESSING DATA STORED
IN AN ARRAY
Using a VALUE clause to initialize an entire
array to 0.
01 ARRAY-1 VALUE ZERO.
05 TOTALS OCCURS 50 TIMES PIC 9(5).
can also be written as:
01 ARRAY-1.
05 TOTALS OCCURS 50 TIMES PIC 9(5) VALUE ZERO.
Setting each element to a different value.
01 MONTH-NAMES
VALUE ‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’.
05 MONTH OCCURS 12 TIMES PIC XXX.
PROCESSING DATA STORED
IN AN ARRAY
Using OCCURS with a VALUE and
REDEFINES clause .
01 MONTH-NAMES.
05 STRING-1 PIC X(36)
VALUE ‘JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC’
05 MONTH REDEFINES STRING-1 OCCURS 12 TIMES PIC XXX.
redefines STRING-1 and enables each three-character abbreviation for months 1 through
12 to be accessed separately using a subscript..
establishes a 36-position constant that contains a three-character abbreviation for each of the
12 months of the year
============================================
SET X1 TO 1
SEARCH TABLE-ENTRIES AT END
MOVE 0 TO WS-SALES-TAX
WHEN ZIP-IN = WS-ZIPXODE(X1)
COMPUTE WS-SALES-TAX ROUNDED = WS-TAX-RATE (X1) *
UNIT-PRICE-IN * QTY-IN
END-SEARCH.
=================================================
USE OF THE SEARCH STATEMENT FOR
TABLE AND ARRAY PROCESSING conttinued
MOVE WS-SALES-TAX TO DL-SALES-TAX.
COMPUTE DL-TOTAL = UNIT-PRICE-IN * QTY-IN + WS-SALES-TAX.
WRITE BILLING-REC FROM DETAIL-LINE AFTER ADVANCING 2 LINES.
The identifier used with the SEARCH verb is the table entry
name specified on the OCCURS level, not on the 01 level.
The WHEN clause indicates what action is to be taken
when the condition specified is actually met
With the SEARCH statement, the AT END clause specifies
what should be done if the table has been completely
searched and no matched is found. It is strongly
recommended that always use this optional clause, without
it, the “no match” condition will simply cause the program to
continue with the next sentence.
USE OF THE SEARCH STATEMENT FOR
TABLE AND ARRAY PROCESSING
THE INDEXED BY clause and the SEARCH Statement.
When using a SEARCH statement , table entries must be
specified with an index rather than a subscript. An index is
similar to a subscript but it is defined along with the table
entries as part of the OCCURS description.
01 SALES-TAX-TABLE
05 TABLE-ENTRIES OCCURS 1000 TIMES INDEXED BY X1
10 WS-ZIPCODE PIC 9(5).
10 WS-TAX-RATE PIC V999.
USE OF THE SEARCH STATEMENT FOR
TABLE AND ARRAY PROCESSING
01 SALES-TAX-TABLE
05 TABLE-ENTRIES OCCURS 1000 TIMES INDEXED BY X1
10 WS-ZIPCODE PIC 9(5).
10 WS-TAX-RATE PIC V999.
SUBSCRIPT
• Represents an occurrence of an array or table
element
• Defined in a separate working-STORAGE entry
• To change the value of SUB, a subscript, use a
PERFORM . . . VARYING or any of the
following
MOVE 1 to SUB
ADD 1 TO SUB
SUBTRACT 1 FROM SUB
USE OF THE SEARCH STATEMENT FOR
TABLE AND ARRAY PROCESSING
INDEX
• Represents a displacement from the first
address in the array or table
• Defined along with the OCCURS for the array or
table
• To change the value of X1, an index, use
PERFORM … VARYING or any of the following:
SET X to 1
SET X1 UP BY 1
SET X1 DOWN BY 1
USE OF THE SEARCH STATEMENT FOR
TABLE AND ARRAY PROCESSING
SET X1 TO 1
SEARCH TABLE-ENTRIES
AT END MOVE 0 TO WS-SALES-TAX
WHEN ZIP-IN = WS-ZIPCODE(X1)
COMPUTE WS-SALES-TAX = WS-TAX-RATE(X1) *
UNIT-PRICE-IN * QTY-IN
END-SEARCH
MOVE
COMPUTE
WRITE
AFTER ADVANCING 2 LINES
MULTIPLE LEVEL OCCURS
Clause
Defining a TWO-DIMENSIONAL array(coding 1):
01 TEMPERATURE-ARRAY. MAJOR-level
occurs clause
05 DAY-IN-WEEK OCCURS 7 TIMES.
10 HOURS OCCURS 24 TIMES MINOR-level
occurs clause
15 TEMP PIC S9(3).
This format is particularly useful for processing multiple-level arrays and tables. The
PERFORM . . VARYING varies the major subscript, and the AFTER clause varies the
minor subscript. Note, however, that for many compilers the AFTER clause requires a
procedure-name1 following the word PERFORM.
END