cobol array processing and tables
TRANSCRIPT
-
8/10/2019 Cobol Array Processing and Tables
1/56
Array Processing and Table
Handling
-
8/10/2019 Cobol Array Processing and Tables
2/56
OBJECTIVES
To familiarize you with:1. How to establish a series of items using an OCCURS clause.
2. How to access and manipulate data stored in an array ortable.
3. The rules for using an OCCURS clause in the DATA DIVISION.4. The use of a SEARCH or SEARCH ALL for a table look- up.
-
8/10/2019 Cobol Array Processing and Tables
3/56
CONTENTS An Introduction to Single-Level OCCURS Clauses
Why OCCURS Clauses Are Used
Rules for Use of the OCCURS Clause
Processing Data Stored in an Array
Using OCCURS with VALUE and REDEFINES Clauses
Printing Data Stored in an Array
-
8/10/2019 Cobol Array Processing and Tables
4/56
CONTENTS
Using an OCCURS Clause for Table Handling
Defining a Table
Storing the Table in WORKING-STORAGE
Looking Up Data in a Table: Finding a Match
Use of the SEARCH Statement for Table and Array Processing
Format of the SEARCH Statement
The INDEXED BY Clause and the SEARCH Statement
Modifying the Contents of an Index
-
8/10/2019 Cobol Array Processing and Tables
5/56
CONTENTS
Using Two WHEN Clauses for an Early Exit from aSEARCH
Searching for Multiple Matches
Internal vs... External Tables
Looking up Table Data for Accumulating Totals
The SEARCH ... VARYING Option for ProcessingParallel Tables
-
8/10/2019 Cobol Array Processing and Tables
6/56
CONTENTS
The SEARCH ALL Statement Definition of a Serial Search
Definition of a Binary Search
Format of the SEARCH ALL Statement
ASCENDING or DESCENDING KEY with the SEARCH ALL Statement
-
8/10/2019 Cobol Array Processing and Tables
7/56
CONTENTS
Multiple-Level OCCURS Clause
Accessing a Double-Level or Two-Dimensional Array
Defining a Double-Level or Two-Dimensional Array
Using PERFORM ... VARYING ... AFTER
Using a Double-Level or Two-Dimensional Array for Accumulating
Totals
Performing a Look-Up Using a Double-Level OCCURS
-
8/10/2019 Cobol Array Processing and Tables
8/56
An Introduction toSingle-Level OCCURS Clauses
-
8/10/2019 Cobol Array Processing and Tables
9/56
Why OCCURS Clauses Are Used
SOME USES OF OCCURS
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.
3. Defining a table in WORKING-STORAGE to be accessed by eachinput record.
With a table, we use the contents of some input filed to "look up" therequired data in the table.
-
8/10/2019 Cobol Array Processing and Tables
10/56
-
8/10/2019 Cobol Array Processing and Tables
11/56
Defining a Subscript
Collectively, these 24 fields within the array are calledTEMPERATURE, which is the identifierused to access them in thethe PROCEDURE DIVISION.
We use the identifier TEMPERATURE along with a subscriptthat
indicates which of the 24 fields we wish to access:DISPLAY TEMPERATURE (23)
-
8/10/2019 Cobol Array Processing and Tables
12/56
SUMMARY OF OCCURS
AND SUBSCRIPTS1. An OCCURSclause is defined in the DATA
DIVISIONto indicate the repeated occurrence ofitems in an array that have the same format.
2. A subscriptis used in the PROCEDURE DIVISIONtoindicate which specific item within the arraywewish to access.
-
8/10/2019 Cobol Array Processing and Tables
13/56
QUESTIONS?
-
8/10/2019 Cobol Array Processing and Tables
14/56
SELF-TESTConsider the following for Questions 1 through 5:
01 IN-REC.05 AMT1 PIC 9(5).
05 AMT2 PIC 9(5).
05 AMT3 PIC 9(5).
05 AMT4 PIC 9(5).
05 AMT5 PIC 9(5).
-
8/10/2019 Cobol Array Processing and Tables
15/56
SELF-TEST
1. An OCCURS clause could be used in place ofdefining each AMT field separately because
____________________________________ .
SOLUTION: all AMTs have the same format or PICclause
-
8/10/2019 Cobol Array Processing and Tables
16/56
SELF-TEST
2. (T or F) Suppose AMT2 and AMT4 had PIC 9(3). AnOCCURS clause could not be used to define allthe AMT fields.
SOLUTION: T
-
8/10/2019 Cobol Array Processing and Tables
17/56
SELF-TEST
3. Recode the fields within IN-REC using an OCCURSclause.
SOLUTION:
01 IN-REC.
05 AMT OCCURS 5 TIMES PIC 9(5).
-
8/10/2019 Cobol Array Processing and Tables
18/56
SELF-TEST
4. To access any of the five items defined with theOCCURS clause, we must use a ____ in thePROCEDURE DIVISION.
SOLUTION: subscript
-
8/10/2019 Cobol Array Processing and Tables
19/56
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 WORKING-STORAGE 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 . . .
-
8/10/2019 Cobol Array Processing and Tables
20/56
-
8/10/2019 Cobol Array Processing and Tables
21/56
Rules for Use of the
OCCURS Clause Levels 02--49
An OCCURS clause may be used on levels 02-- 49only.
That is, the OCCURS is not valid for the 01 level sinceit must be used for defining fields, not records.
Elementary or Group Items may also be definedwith an OCCURS Clause.
-
8/10/2019 Cobol Array Processing and Tables
22/56
-
8/10/2019 Cobol Array Processing and Tables
23/56
Using OCCURS with VALUE
and REDEFINES Clauses Sometimes we want to initialize elements in a
table or an array with specific values.
We have seen that with COBOL 85 you can use a
VALUE clause to set an entire array to zero:
01 ARRAY-1 VALUE ZERO.05 TOTALS OCCURS 50 TIMES PIC 9(5).
-
8/10/2019 Cobol Array Processing and Tables
24/56
Using an OCCURS Clause forTable Handling
-
8/10/2019 Cobol Array Processing and Tables
25/56
Defining a Table
A tableis a list of stored fields that are looked upor referenced by the program.
Tables are used in conjunction with table look-ups,
where a table look-upis a procedure that finds aspecific entry in the table.
Thus, an arraystores data or totals to beoutputted, whereas a tableis used for looking upor referencing data.
-
8/10/2019 Cobol Array Processing and Tables
26/56
Storing the Table in
WORKING-STORAGE Storing data in a table file rather than in each transaction
record is more efficient because it minimizes data entryoperations.
It is also more efficient because the data can be more easilymaintained, or updated, in a separate table, as needed.
Any time input records need to be saved for future processing,use a READINTO to store the data in WORKING-STORAGE.
-
8/10/2019 Cobol Array Processing and Tables
27/56
Use of the SEARCH Statement forTable and Array Processing
-
8/10/2019 Cobol Array Processing and Tables
28/56
Format of the SEARCH
StatementSEARCH identifier-1
[AT END imperative-statement-1]
WHEN condition-1 {imperative- statement-2} {NEXT
SENTENCE} ...
[END-SEARCH]*
*COBOL 85 only. If END-SEARCHis used, NEXT SENTENCEmust bereplaced with CONTINUEunless your COBOL 85 compiler has an
enhancement that permits it.
-
8/10/2019 Cobol Array Processing and Tables
29/56
Using the SEARCH ... AT END forData Validation
With the SEARCH statement, the AT END clause specifies whatshould be done if the table has been completely searched andno match is found.
Since it is possible for input errors to occur, we strongly
recommend that you always use this optional clause. Without it, the ``no match'' condition would simply cause the
program to continue with the next sentence; thus producingincorrect results or even a program interrupt.
-
8/10/2019 Cobol Array Processing and Tables
30/56
The INDEXED BY Clause andthe SEARCH Statement When using a SEARCHstatement, table entries must be specified
with an index rather than a subscript.
An indexis similar to a subscript, but it is defined along with thetable entries as part of the OCCURS description:
01 SALES-TAX-TABLE.05 TABLE-ENTRIES OCCURS1000 TIMES
INDEXED BYX1.10 WS-ZIPCODE PIC 9(5).10 WS-TAX-RATE PIC V999.
H I d Diff F S b i t
-
8/10/2019 Cobol Array Processing and Tables
31/56
How an Index Differs From a Subscript
Indexes are processed differently and more efficiently than
subscripts. When you define an index, the computer sets up an internal
storage area called an index register.
Registers use the displacement values determined by the index toaccess table addresses.
This is faster than working with subscripts.
We recommend that you use indexes and SEARCH statements fortable look-ups.
iff i
-
8/10/2019 Cobol Array Processing and Tables
32/56
How an Index Differs From a Subscript
Because an index refers to a displacement and not just anoccurrence value, its contents cannot be modified with a MOVE,ADD, or SUBTRACT like a subscript can.
To change the contents of an index, then, we use either:
(1) a PERFORM ... VARYING, which can vary the values in eithersubscripts or indexes.
(2) a SETstatement, which can move, add, or subtract values in anindex.
-
8/10/2019 Cobol Array Processing and Tables
33/56
Modifying the Contents of anIndex: The SET StatementBasic Format:
SET index-name-1 {TO} {UP BY} {DOWN BY} integer-1
EXAMPLES:
Statement Meaning1. SET X1 TO 1 Move 1 to the X1 index.
2. SET X1 UP BY 1 Add 1 to the X1 index.
3. SET X1 DOWN BY 1 Subtract 1 from the X1 index.
Initializing an Index Before Using the
-
8/10/2019 Cobol Array Processing and Tables
34/56
Initializing an Index Before Using theSEARCH
A SEARCHstatement does not automatically initialize the
index at 1 because sometimes we may want to beginsearching a table at some point other than the beginning.
Initializing an index at 1 must be performed by a SETstatementprior to the SEARCHif we want to begin each table look-up withthe first entry; for example: SETX1TO1
DIFFERENCES BETWEEN
-
8/10/2019 Cobol Array Processing and Tables
35/56
DIFFERENCES BETWEENSUBSCRIPTS AND INDEXES
Subscript represents an occurrence of an array or table element.
Defined in a separate WORKING-STORAGE entry.
To change a subscript's value, use a PERFORM ... VARYINGor any
of the following:MOVE 1 TO SUB ADD 1 TO SUB
SUBTRACT 1 FROM SUB
-
8/10/2019 Cobol Array Processing and Tables
36/56
-
8/10/2019 Cobol Array Processing and Tables
37/56
THE SEARCH ... VARYING OPTION FORPROCESSING PARALLEL TABLES
Format:SEARCH identifier-1 VARYING
{identifier-2} {index-name-1}[AT END imperative-statement-1]
{WHEN condition-1 {imperative-statement-2}{NEXT SENTENCE}[END-SEARCH]*
*COBOL 85 only. The NEXT SENTENCEclause is not
permitted with the END-SEARCHunless yourcompiler has an enhancement that allows it.
-
8/10/2019 Cobol Array Processing and Tables
38/56
-
8/10/2019 Cobol Array Processing and Tables
39/56
Definition of a Serial
Search1. The first entry in the table is searched.2. If the condition is met, the table look-up is completed.
3. If the condition is not met, the index or subscript is incremented by
one, and the next entry is searched.4. This procedure is continued until a match is found or the table has
been completely searched.
-
8/10/2019 Cobol Array Processing and Tables
40/56
Definition of a Serial Search
A sequentialor serial search, as described here, is best usedwhen either:
1. The entries in a table are not in either ascending or descendingsequence; that is, they are arranged randomly; or,
2. Table entries are organized so that the first values are the onesencountered most frequently;
in this way, access time is minimized because you are apt to end thesearch after the first few comparisons.
-
8/10/2019 Cobol Array Processing and Tables
41/56
Definition of a Binary
Search When table entries are arranged in sequence by
some field, such as T- CUSTOMER-NO, the mostefficient type of look-up is a binary search.
The following is the way the computer performs abinary search:
-
8/10/2019 Cobol Array Processing and Tables
42/56
Definition of a Binary Search
1. Begin by comparing CUST-NO of the inputcustomer record to the middle table argumentfor T-CUSTOMER-NO. For example, this might be the twenty-fifth entry in
the table.2. Since T-CUSTOMER-NOs are in sequence, if
CUST-NO-IN > T- CUSTOMER-NO (25) - which isthe middle entry in our table - this eliminates theneed for searching the first half of the table. In such a case, we compare CUST-NO-IN to T-
CUSTOMER-NO (37), the middle table argument ofthe second half of the table (rounding down), andcontinue our comparison in this way.
-
8/10/2019 Cobol Array Processing and Tables
43/56
Definition of a Binary
Search3. 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) whenthe table has been completely searched.
-
8/10/2019 Cobol Array Processing and Tables
44/56
Uses of a Binary Search
1. When table entries are arranged in some sequence - eitherascending or descending.
2. When tables with a large number of sequential entries (e.g., 50 ormore) are to be looked up or searched.
For small tables or those in which entries are not arranged in asequence, the standard serial search look-up method previouslydescribed is used.
Format for SEARCH ALL
-
8/10/2019 Cobol Array Processing and Tables
45/56
Format for SEARCH ALL
SEARCH ALL identifier-1
[AT END imperative-statement-1]WHEN {data-name-1 { IS EQUAL TO} {IS =}
{identifier-2} {literal-1} {arithmetic-expression-1}}
{condition-name-1
[AND {data-name-2 {IS EQUAL TO} {IS =}{identifier-3}{literal-2}{arithmetic-expression-2}}]
{condition-name-2} . . .
{imperative-statement-2} {NEXT SENTENCE}
[END-SEARCH]* Cobol 85 only. NEXT SENTENCE cannottypically be used with END-SEARCH.
-
8/10/2019 Cobol Array Processing and Tables
46/56
Limitations of the SEARCH ALLStatement
1. The condition following the word WHEN can testonly for equality:
Valid: WHEN T-CUSTOMER-NO (X1) = CUST-NO-IN
Invalid: WHEN T-WEIGHT-MAX (X1) < WEIGHT-MAILED
-
8/10/2019 Cobol Array Processing and Tables
47/56
Limitations of the SEARCH ALLStatement
2. If the condition following the word WHEN is a compoundconditional:
a. Each part of the conditional can only consist of a relational test that
involves an equal condition.
b. The only compound condition permitted is with ANDs, not ORs.
Valid: WHEN S-AMT (X1) = AMT1 AND TAX-AMT (X1) = AMT2
Invalid: WHEN SALES-AMT (X1) = AMT3 OR AMT4 = AMT5
-
8/10/2019 Cobol Array Processing and Tables
48/56
Limitations of the SEARCH
ALL Statement3. Only one WHEN clause can be used with a SEARCH ALL.
4. The VARYING option may not be used with the SEARCH ALL.
5. The OCCURS item and its index, which define the table argument,must appear to the left of the equal sign.
Valid: WHEN S-AMT (X1) = AMT1...
Invalid: WHEN AMT1 = S-AMT (X1)...
-
8/10/2019 Cobol Array Processing and Tables
49/56
ASCENDING or DESCENDING KEYwith the SEARCH ALL Statement
1. To use the SEARCH ALLstatement, we must indicate which tableentry will serve as the key field.
2. That is, we specify the table entry that will be in sequence so thatthe binary search can be used to compare against that field.
3. We must indicate whether that KEYis ASCENDINGorDESCENDING.
-
8/10/2019 Cobol Array Processing and Tables
50/56
ASCENDING or DESCENDING KEYwith the SEARCH ALL Statement
ASCENDING KEY - Entries are in sequence and increasing in value.
DESCENDING KEY - Entries are in sequence and decreasing in value.Format:
(level-number 02--49) identifier-1
OCCURS integer-1 TIMES{ASCENDING} {DESCENDING} KEY IS
data-name-2INDEXED BY index-name-1
-
8/10/2019 Cobol Array Processing and Tables
51/56
Differences Between the SEARCH
-
8/10/2019 Cobol Array Processing and Tables
52/56
Differences Between the SEARCHand the SEARCH ALLSEARCH ALL
Performs a binary search
Tables entries must be in sequence by the table argument or eventhe table function. The field that is in sequence is specified in anASCENDING or DESCENDING KEY clause as part of the OCCURS entry
Does not need a SET prior to the SEARCH ALL
Can only have a single = condition tested with the WHEN clause
May only have one WHEN clause
-
8/10/2019 Cobol Array Processing and Tables
53/56
MULTIPLE-LEVEL OCCURS
CLAUSE When describing an area of storage, more than one level of
OCCURS may be used.
As many as seven levels of OCCURS are permitted with COBOL
85, and as many as three levels are permitted with COBOL 74. Like a single-level OCCURS, multiple levels of OCCURS may be
used for:
(1) accumulating totals in an array.
(2) storing a table for ``look-up'' purposes.
RULES FOR USING A DOUBLE-LEVEL
-
8/10/2019 Cobol Array Processing and Tables
54/56
RULES FOR USING A DOUBLE LEVELOCCURS
1. If an item is defined by a double-level OCCURS clause, it must beaccessed by two subscripts.
2. The first subscript refers to the higher-level OCCURS; the secondsubscript refers to the lower-level OCCURS.
3. The subscripts must be enclosed in parentheses.
RULES FOR USING A DOUBLE-LEVEL
-
8/10/2019 Cobol Array Processing and Tables
55/56
RULES FOR USING A DOUBLE LEVELOCCURS
4. Subscripts may consist of positive integers or data-names withpositive integer contents.
5. On most systems, the left parenthesis must be preceded by atleast one space; similarly, the right parenthesis must be followed
by a period, if it is the end of a sentence, or at least one space. The first subscript within parentheses is followed by a comma and a
space.
Expanded Format for Accessing a
-
8/10/2019 Cobol Array Processing and Tables
56/56
Expanded Format for Accessing aDouble-Level orTwo-Dimensional Array
PERFORM [procedure-name-1 [{THROUGH} {THRU} procedure-name-2]
[WITH TEST {BEFORE} {AFTER}]
VARYING {identifier-2} {index-name- 1} FROM{identifier-3}{index-name-2} {literal-1}
BY {identifier-4} {literal-2} 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]*