260 likes | 341 Views
Chapter 11. Introduction to Tables. Tables / Arrays - A Matter of Semantics. Arrays used to store multiple occurrences of a set data items Can represent data in an input/output record Used in working storage to establish a set of like accumulators
E N D
Chapter 11 Introduction to Tables
Tables / Arrays - A Matter of Semantics • Arrays used to store multiple occurrences of a set data items Can represent data in an input/output record Used in working storage to establish a set of like accumulators • Tables used to retrieve some reference value need for processing • Both handled the same way in programming
Problem - Represent 12 Months Sales 01 ANNUAL-SALES-DATA. 05 JAN-SALES PIC 9(6). 05 FEB-SALES PIC 9(6). 05 MAR-SALES PIC 9(6). 05 APR-SALES PIC 9(6). 05 MAY-SALES PIC 9(6). 05 JUN-SALES PIC 9(6). 05 JUL-SALES PIC 9(6). 05 AUG-SALES PIC 9(6). 05 SEP-SALES PIC 9(6). 05 OCT-SALES PIC 9(6). 05 NOV-SALES PIC 9(6). 05 DEC-SALES PIC 9(6).
Accumulate Total-Sales COMPUTE TOTAL-SALES = JAN-SALES + FEB-SALES + MAR-SALES + APR-SALES + MAY-SALES + JUN-SALES + JUL-SALES + AUG-SALES + SEP-SALES + OCT-SALES + NOV-SALES + DEC-SALESIs there a better way?
Occurs Clause • Provides for grouping similar data • Represents multiple occurrences same type • Format is the same (ie. Picture) for each item • Allows use of same data name • Individual data items identified by a subscript or index • OCCURS integer TIMES • Cannot be used at 01 level
ANNUAL-SALES-DATA SALES (1) SALES (2) SALES (12) . . . The Table Concept 01 ANNUAL-SALES-DATA. 05 SALES OCCURS 12 TIMES PIC 9(6). (b) OCCURS Clause (c) Memory Storage Schematic
ANNUAL-SALES-DATA SALES (1) SALES (2) SALES (12) . . . Figure 11.1 The Table Concept 01 ANNUAL-SALES-DATA. 05 JAN-SALES PIC 9(6). 05 FEB-SALES PIC 9(6). 05 MAR-SALES PIC 9(6). 05 APR-SALES PIC 9(6). 05 MAY-SALES PIC 9(6). 05 JUN-SALES PIC 9(6). 05 JUL-SALES PIC 9(6). 05 AUG-SALES PIC 9(6). 05 SEP-SALES PIC 9(6). 05 OCT-SALES PIC 9(6). 05 NOV-SALES PIC 9(6). 05 DEC-SALES PIC 9(6). (a) Brute Force 01 ANNUAL-SALES-DATA. 05 SALES OCCURS 12 TIMES PIC 9(6). (b) OCCURS Clause (c) Storage Schematic
PERFORM VARYING PERFORM [procedure name] [WITH TEST {BEFORE/ AFTER}] <= BEFORE is Default VARYING identifier-1 FROM {literal-1 / identifier-2} BY {literal-2 / identifier-3} UNTIL condition-1 [imperative-statement(s) END-PERFORMidentifier-1 define in ws - we refer to it as a subscript{literal-1 / identifier-2} an integer value defining starting point{literal-2 / identifier-3} an integer value defining increment amount
PERFORM VARYING steps (with TEST BEFORE) • Initializes the variable • Tests the condition • Enters the loop • Increments the variable • Retests the variable
Initializeidentifier-1 to FROM value Increment identifier-1 with BY value Perform procedure-1 or imperative- statement Evaluate condition=1 FALSE TRUE Figure 11.2 PERFORM VARYING (with TEST BEFORE)
Figure 11.3 Processing a Table MOVE ZERO TO ANNUAL-TOTAL.PERFORM 400-INCREMENT-ANNUAL-TOTAL VARYING SALES-SUB FROM 1 BY 1 UNTIL SALES-SUB > 12. . .400-INCREMENT-ANNUAL-TOTAL. ADD SALES (SALES-SUB) TO ANNUAL-TOTAL. (a) Performing a Paragraph MOVE ZERO TO ANNUAL-TOTAL.PERFORM VARYING SALES-SUB FROM 1 BY 1 UNTIL SALES-SUB > 12 ADD SALES (SALES-SUB) TO ANNUAL-TOTALEND-PERFORM. (b) In-line Perform
Problem Definition We need to define a group of data items that repeat - such as Salaryand Salary effective dateWant to keep track of current salary and previous threesalaries
SALARY-DATA (2) SALARY-DATA (1) SALARY-DATA (3) SALARY (2) SALARY (1) SALARY (3) SAL-DATE (2) SAL-DATE (1) SAL-DATE (3) Figure 11.4 OCCURS Clause at the Group Level 05 SALARY-DATA OCCURS 4 TIMES. 10 SALARY PIC 9(6)V99. 10 SAL-DATE PIC 9(4). (a) COBOL Statements (b) Storage Schematic
SALARY-DATA SALARY (1) SAL-DATE (1) SALARY (2) SAL-DATE (2) SALARY (3) SAL-DATE (3) Figure 11.5 OCCURS Clause at the Elementary Level 05 SALARY-DATA. 10 SALARY OCCURS 4 TIMES PIC 9(6)V99. 10 SAL-DATE OCCURS 4 TIMES PIC 9(4). (a) COBOL Statements (b) Storage Schematic
SUBSCRIPTS • Defined in WORKING-STORAGE • USAGE COMP - internal representation binary • Syntax DATE (2) or DATE (Month-No) • Relative Subscripting BONUS (YEARS + 2)
CAVEATS • Data defined with an OCCURS must be referenced with a subscript or index • Syntax error if no subscript used • Runtime error if subscript 0 or greater than table size
Report Specifications SALARY REPORT FORXXXXXXXXXXXXXXXXXXXXXXXXXXXXCURRENT SALARY EFFECTIVE DATE PERCENT INCREASE $46,000 09/93 15.0% $40,000 09/92 11.1% $36,000 09/91 12.5% $32,000 09/90
Figure 11.6 Relative Subscripting PERFORM VARYING SUB FROM 1 BY 1 UNTIL SUB > 3 OR SALARY (SUB + 1) = 0 COMPUTE PCT-SALARY-INC (SUB) = 100 * ((SALARY (SUB) - SALARY (SUB+1)) / SALARY (SUB + 1) END-COMPUTEEND-PERFORM (b) Computation of Percent Salary Increase
OCCURS DEPENDING ON • Variable length recordsOCCURS {integer-1 TO integer-2} DEPENDING ON data-name
Figure 11.7 Variable-length Records FD STUDENT-TRANSCRIPT-FILE RECORD CONTAINS 42 TO 1131 CHARACTERS DATA RECORD IS STUDENT-RECORD.01 STUDETN-RECORD. 05 ST-NAME PIC X(30). 05 ST-MAJOR PIC X(10). 05 ST-COURSES-COMPLETED PIC 99. 05 ST-COURSE-INFO OCCURS 0 TO 99 TIMES DEPENDING ON ST-COURSES-COMPLETED. 10 ST-COURSE-NUMBER PIC 9(6). 10 ST-GRADE PIC X. 10 ST-COURSE-DATE PIC 9(4).
Depending On 05 ST-COURSES-COMPLETED PIC 99. 05 ST-COURSE-INFO OCCURS 0 TO 99 TIMES DEPENDING ON ST-COURSES-COMPLETED. 10 ST-COURSE-NUMBER PIC 9(6). 10 ST-GRADE PIC X. 10 ST-COURSE-DATE PIC 9(4).
Figure 11.9 Calculation of Grade Point Average COURSE COURSE GRADE COURSE CREDITS Course Number 1 A 2 Course Number 2 B 4 (a) Hypothetical Grades SUB GRADE (SUB) CREDITS (SUB) MULTIPLIER TOTAL-QUALITY-POINTS TOTAL-CREDITS 1 A 2 4 8 (0 + 2*4) 2 2 B 4 3 20 (8 + 4*3) 6 (b) Incrementing Counters GRADE-POINT-AVERAGE = TOTAL-QUALITY-POINTS / TOTAL-CREDITS = 20 / 6 = 3.33 (c) Calculation of Grade Point Average
Open filesDO WHILE data remains READ Student file AT END Indicate no more data NOT AT END Write transcript heading Move zero to quality-point and credit counters DO for each course Determine multiplier for this course Increment total quality points Increment total credit counter Write detail line for this course ENDDO COMPUTE grade-point-average = total quality points /’ total credits Write grade-point-average IF dean’s list IF students on dean’s list > 100 Display appropriate error message ELSE Increment students on dean’s list Move this student to dean’s list table END-IF END-IF ENDREADENDDOWrite heading for dean’s listDO for every student on dean’s list Write student dataENDDOClose filesStop run Figure 11.11 Pseudocode for Transcript Program
Defined as part of the OCCURS clause More efficient than subscriptsOCCURS integer-1 TIMES [ASCENDING/DESCENDING KEY IS data-name][INDEXED BY index-name-1] Used the same as a Subscript Requires no other definition Must use SET verb INDEXES
Table 11.1 Indexes versus Subscripts INDEXES SUBSCRIPTS Defined with a specific table; can be used Defined in Working-Storage; the same subscript only with the table with which they are defined can be used with multiple tables although this is not recommended Initialized and incremented via the SET May not be used with SET statements (MOVE and can also be manipulated in ADD are used instead); can also be manipulated in PERFORM statements Provide more efficient object code than USAGE IS COMPUTATIONAL makes subscriptssubscripts more efficient, although indexes are still faster