Sie sind auf Seite 1von 32

Array Processing and Table 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 SEARCHVARYING 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 WORKINGSTORAGE 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).

Defining a Subscript:

ONE SPACE IS TYPICALLY REQUIRED

MOVE TEMPERATURE (2) TO TEMP-OUT DISPLAY TEMPERATURE (23)

INTRO to SINGLE-LEVEL OCCURS Clause


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( [1] data-name with numeric, integer value or [2] a numeric literal with an integer value) is used in the PROCEDURE DIVISION to indicate which specific item within the array we wish to access.

SINGLE-LEVEL OCCURS Clause Example


IDENTIFICATION DIVISION. PROGRAM-ID. SINGLE-DIMENSION-ARRAYS. DATA DIVISION. WORKING-STORAGE SECTION. 01 TEMP-RECORD. 05 TEMPERATURE OCCURS 12 TIMES PIC 9(3)V99. 01 TOTAL-TEMP PIC 9(4)V99. 01 AVG-TEMP PIC 9(4)V99. 01 SUB PIC 99.

SINGLE-LEVEL OCCURS Clause Example continued


PROCEDURE DIVISION. MAIN-PARAGRAPH. MOVE ZEROES TO TOTAL-TEMP. PERFORM 500-ADD-RTN VARYING SUB FROM 1 BY 1 UNTIL SUB > 12. DIVIDE TOTAL-TEMP BY 12 GIVING AVG-TEMP. DISPLAY AVG-TEMP AT LINE 20 COLUMN 1. STOP RUN. 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 In either case. MONTH (SUB) contains a three-character month abbreviation.

USING AN OCCUR CLAUSE IN TABLE HANDLING


A table is a list of stored fields that are looked up or referenced by the program. It is used in conjunction with table look-ups (a procedure that finds a specific entry in the table). An array stores data or totals to be produced as output, whereas a table is used for looking up or referencing data.

USING AN OCCURS Clause FOR TABLE HANDLING


Why TABLES are Used? Scenario: Suppose that a mail-order company ships items to customers throughout US. A program is required that (1) reads customer input data containing billing information and (2) produces output in the form of bills. Since each county within the U.S has different local tax structure, a procedure must be established for calculating sales tax.

USING an OCCURS Clause for TABLE HANDLING continued. . .


Two techniques may be employed: 1. The actual sales tax rate may be entered as part of each input record.
Entering sales tax rate in each input record would be very inefficient.

Sales tax rates occasionally change- each time there is a change to a tax rate in a county, all input records for that county would need to be changed Recording the sales tax rate for each input record means extra keying. If 100 records all pertain to a single county, we would be entering the same sales tax rate 1000 times additional labor and added risk of input errors.

USING OCCURS Clause for TABLE HANDLING


2. The sales tax rate may be entered and stored in a table, which can be referenced for lookup.
This is far the most efficient and effective method for storing tax rate data than the first method. Input to the program would consists of two files. The table file with the sales tax rates corresponding to each county is entered as the first file and stored in WORKINGSTORAGE. Then the input transaction file is read; for each input transaction record, we would find or look up the sales tax rate in the table that corresponds to the county specified in the input record

USING OCCURS Clause for TABLE HANDLING Example:

RULES FOR USE OF THE OCCURS CLAUSE


LOOKING UP data in a TABLE Finding a MATCH See example:USING STORING TABLE IN A WORKING STORAGE SECTION USING DATA FILE.

USE OF THE SEARCH STATEMENT FOR TABLE AND ARRAY PROCESSING


The best method for searching a table.
Format:

SEARCH identifier-1 [ AT END imperative-statement1] WHEN condition1 ] imperative-statement2 ..... CONTINUE [END-SEARCH]

USE OF THE SEARCH STATEMENT FOR TABLE AND ARRAY PROCESSING


We can replace 300-CALC-RTN and 400-INCREMENTSUBSCRIPT
300-CALC-RTN.
MOVE CUST-NO-IN TO DL-CUST-NO-OUT MOVE UNIT-PRICE-IN TO DL-UNIT-PRICE-OUT MOVE QTY-IN TO DL-QTY-OUT

============================================ 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.

The SEARCH statement will perform a table look-up, TABLE-ENTRIES, the identifier used with the OCCURS and INDEXED BY clauses, is the item designated with the SEARCH as well. 01 SALES-TAX-TABLE will not be used with the SEARCH. The table will be searched and the index automatically incremented until the condition specified is satisfied or until AT END is met. The AT END indicates that the table has been completely searched without the condition being met; that is, no match has been found between an input field (search argument) and a table entry (table argument).

USE OF THE SEARCH STATEMENT FOR TABLE AND ARRAY PROCESSING


How INDEXED DIFFERS FROM A SUBSCRIPT Indexes are processed more efficiently than subscripts. A subscript is a field that refers to the number of the table entry we want to reference. An index refers to the displacement.

USE OF THE SEARCH STATEMENT FOR TABLE AND ARRAY PROCESSING


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


MODIFYING THE CONTENTS OF AN INDEX Format: SET index-name1 TO UP BY integer-1 DOWN BY Example: Statement Meaning
1. 2. 3.

SET X1 TO 1 SET X1 UP BY 1 SET X1 DOWN BY 1

Move 1 to the X1 index Add 1 to the X1 index Subtract 1 from the X1 index

USE OF THE SEARCH STATEMENT FOR TABLE AND ARRAY PROCESSING


Initializing and INDEX before Using the SEARCH A SEARCH statement does not automatically initialize the index at 1 because sometimes we want to begin searching a table at some point other than the beginning. Initializing an index at 1 must be performed by a SET statement prior to the SEARCH if we want to begin each table look-up with the first entry.

MULTIPLE LEVEL OCCURS


Multiple level OCCURS may be used for 1. Accumulating totals in an array 2. Storing a table for look-up purposes.

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.

05 DAY-IN-WEEK OCCURS 7 TIMES.


10 HOURS OCCURS 24 TIMES 15 TEMP PIC S9(3).

MAJOR-level occurs clause MINOR-level occurs clause

This two-dimensional array is established as follows: 1. The arrays will have 7 rows as indicated by the first OCCURS clause. 2. Within this array, each row will have 24 columns, as indicated by the second OCCURS clause. 3. Each of the elements in this 7 x 24 array will be large enough to hold three integers, as indicated by the subordinate entry.

MULTIPLE LEVEL OCCURS Clause


ACCESSING A TWO-DIMENSIONAL ARRAY.
Can alternatively (coding 2) written as:

01 TEMPERATURE-ARRAY. 05 DAY-IN-WEEK OCCURS 7 TIMES. 10 HOURS OCCURS 24 TIMES PIC S9(3). To access any of the element , we use the data-name on the lowest OCCURS level or any data-name subordinate to it. Either TEMP (in coding 1) or HOUR (coding 2) could be used to access the elements of the array.

MULTIPLE LEVEL OCCURS Clause Example


Suppose we wish to print an average temperature for the entire week. We need to add all the array entries to a total and divide by 168 (7 x 24) . We can use nested PERFORMs for this purpose. 800-AVERAGE-RTN. MOVE 0 TO TOTAL. PERFORM VARYING DAY-SUB FROM 1 BY 1 UNTIL DAY-SUB > 7
PERFORM VARYING HOUR-SUB FROM 1 BY 1 UNTIL HOUR-SUB > 24 ADD TEMP (DAY-SUB , HOUR SUB ) TO TOTAL END-PERFORM END-PERFORM.

COMPUTE WEEKLY-AVERAGE= TOTAL/168 WRITE PRINT-REC FROM OUT-REC AFTER ADVANCING 2 LINES.

MULTIPLE LEVEL OCCURS Clause Example Alternative Code


The PERFORM . . . VARYING WITH THE AFTER OPTION 600-AVERAGE-RTN. MOVE 0 TO TOTAL.
PERFORM 700-LOOP1 VARYING DAY-SUB FROM 1 BY 1 UNTIL DAY-SUB > 7 AFTER HOUR-SUB FROM 1 BY 1 UNTIL HOUR-SUB > 24 COMPUTE WEEKLY-AVERAGE= TOTAL/168 WRITE PRINT-REC FROM OUT-REC AFTER ADVANCING 2 LINES. 700-LOOP1. ADD TEMP (DAY-SUB , HOUR SUB ) TO TOTAL.
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

Das könnte Ihnen auch gefallen