Documente Academic
Documente Profesional
Documente Cultură
Copyright @ 2000 John Wiley & Sons, In. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express permission of the copyright owner is unlawful. Request for further information should be addressed to the permissions Department , John Wily & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein.
9th Edition
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 or table. 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.
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
An Introduction to Single-Level OCCURS Clauses
Why OCCURS Clauses Are Used Rules for Use of the OCCURS Clause
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
CONTENTS
Using Two WHEN Clauses for an Early Exit from a SEARCH Searching for Multiple Matches Internal vs... External Tables
Looking up Table Data for Accumulating Totals The SEARCH ... VARYING Option for Processing Parallel Tables
Structured COBOL Programming, Stern & Stern, 9th Edition
CONTENTS
The SEARCH ALL Statement
Definition of a Serial Search Definition of a Binary Search Format of the SEARCH ALL Statement
CONTENTS
Multiple-Level OCCURS Clause
Accessing a Double-Level or Two-Dimensional Array Defining a Double-Level or Two-Dimensional Array
Defining a Subscript
Collectively, these 24 fields within the array are called TEMPERATURE, which is the identifier used to access them in the the PROCEDURE DIVISION.
We use the identifier TEMPERATURE along with a subscript that indicates which of the 24 fields we wish to access:
DISPLAY TEMPERATURE (23)
Structured COBOL Programming, Stern & Stern, 9th Edition
2. A subscript is used in the PROCEDURE DIVISION to indicate which specific item within the array we wish to access.
QUESTIONS?
SELF-TEST
Consider the following for Questions 1 through 5:
01 IN-REC.
05 AMT1 PIC 9(5).
05 AMT2
05 AMT3 05 AMT4 05 AMT5
PIC 9(5).
PIC 9(5). PIC 9(5). PIC 9(5).
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
Structured COBOL Programming, Stern & Stern, 9th Edition
SELF-TEST
3. Recode the fields within IN-REC using an OCCURS clause.
SOLUTION:
01 IN-REC.
05 AMT OCCURS 5 TIMES
Structured COBOL Programming, Stern & Stern, 9th Edition
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
Structured COBOL Programming, Stern & Stern, 9th Edition
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 ...
Structured COBOL Programming, Stern & Stern, 9th Edition
An OCCURS clause may be used to define fields either in the FILE SECTION or in WORKING-STORAGE.
Structured COBOL Programming, Stern & Stern, 9th Edition
Defining a Table
A table is a list of stored fields that are looked up or referenced by the program. Tables are used in conjunction with table look-ups, where a table look-up is a procedure that finds a specific entry in the table. Thus, an array stores data or totals to be outputted, whereas a table is used for looking up or referencing data.
Structured COBOL Programming, Stern & Stern, 9th Edition
Any time input records need to be saved for future processing, use a READINTO to store the data in WORKING-STORAGE.
Structured COBOL Programming, Stern & Stern, 9th Edition
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 to access table addresses.
This is faster than working with subscripts.
We recommend that you use indexes and SEARCH statements for table look-ups.
Structured COBOL Programming, Stern & Stern, 9th Edition
To change a subscript's value, use a PERFORM ... VARYING or any of the following:
To change an index value, use a PERFORM ... VARYING or any of the following:
1. The entries in a table are not in either ascending or descending sequence; that is, they are arranged randomly; or, 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.
Structured COBOL Programming, Stern & Stern, 9th Edition
2. Since T-CUSTOMER-NOs are in sequence, if CUST-NO-IN > T- CUSTOMER-NO (25) which is the middle entry in our table - this eliminates the need for searching the first half of the table.
In 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.
Structured COBOL Programming, Stern & Stern, 9th Edition
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.
Structured COBOL Programming, Stern & Stern, 9th Edition
{identifier-3}{literal-2}{arithmetic-expression-2}}
{condition-name-2} . . . {imperative-statement-2} {NEXT SENTENCE}
[END-SEARCH]* Cobol 85 only. NEXT SENTENCE cannot typically be used with END-SEARCH.
Structured COBOL Programming, Stern & Stern, 9th Edition
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)...
Structured COBOL Programming, Stern & Stern, 9th Edition
(level-number 02--49) identifier-1 OCCURS integer-1 TIMES {ASCENDING} {DESCENDING} KEY IS data-name-2 INDEXED BY index-name-1
Structured COBOL Programming, Stern & Stern, 9th Edition
Can only have a single = condition tested with the WHEN clause
May only have one WHEN clause
Structured COBOL Programming, Stern & Stern, 9th Edition
One of the supplements includes more information on multiple-level arrays that you may download
CHAPTER SUMMARY
A. OCCURS clauses are used in the DATA DIVISION to specify the repeated occurrence of items with the same format.
1. OCCURS clauses may be written on levels 02-49.
CHAPTER SUMMARY
C. Use of the SEARCH statement for table handling:
1. The identifier used with the SEARCH verb is the one specified on the OCCURS level. 2. The AT END clause specifies what is to be done if the table has been searched and the required condition has not been met. 3. The WHEN clause indicates what to do when the condition is met.
CHAPTER SUMMARY
4. When using a SEARCH statement, table entries are specified with the use of an index, rather than a subscript.
a. The index is defined along with the OCCURS. For example:
01 UNIT-PRICE-TABLE. 05 STORED-ENTRIES OCCURS 500 TIMES INDEXED BY X1.
b. An index cannot be modified with a MOVE, ADD, or SUBTRACT statement. Use a SET statement when altering the contents of an index, or use the PERFORM ... VARYING. c. SET the index to 1 before using a SEARCH. d. Use a PERFORM ... VARYING to load the table.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
D. The SEARCH ALL statement - uses and limitations.
1. Used to perform a binary search.
2. Can test only an equal condition. 3. If using a compound condition: (a) each part can only test an equal condition and (b) only ANDs are permitted. 4. Only one WHEN clause can be used.
5. The ASCENDING or DESCENDING KEY is specified along with the OCCURS and INDEXED BY clauses of a table entry.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
E. Multiple-level OCCURS
1. May be used for an array or a table. 2. The lowest-level OCCURS data-name or an item subordinate to it is used to access an entry in the array or the table. 3. If we use a SEARCH for accessing a multiplelevel table, INDEXED BY must be used on all OCCURS levels. 4. The identifier used with the SEARCH statement should typically be the one on the lowest OCCURS level.
Only the index on the same level as the OCCURS level will be incremented by the SEARCH.
Structured COBOL Programming, Stern & Stern, 9th Edition
CHAPTER SUMMARY
F. COBOL 85 permits seven levels of OCCURS; COBOL 74 permits three levels.