Post on 31-Dec-2015
description
transcript
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-SALES
Is 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 indexOCCURS integer TIMESCannot be used at 01 level
The Table Concept
01 ANNUAL-SALES-DATA. 05 SALES OCCURS 12 TIMES PIC 9(6).
(b) OCCURS Clause
ANNUAL-SALES-DATA
SALES (1) SALES (2) SALES (12)
. . .
(c) Memory Storage Schematic
Figure 11.1 The Table Concept01 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
ANNUAL-SALES-DATA
SALES (1) SALES (2) SALES (12)
. . .
(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-PERFORM
identifier-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 variableTests the conditionEnters the loopIncrements the variableRetests the variable
Figure 11.2 PERFORM VARYING (with TEST BEFORE)
Initializeidentifier-1
to FROM value
Evaluatecondition=1
Performprocedure-1
or imperative-statement
Incrementidentifier-1
with BY value
TRUE
FALSE
Figure 11.3 Processing a Table
MOVE ZERO TO ANNUAL-TOTAL.PERFORM 400-INCREMENT-ANNUAL-TOTAL
VARYING SALES-SUB FROM 1 BY 1UNTIL 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 1UNTIL 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 date
Want to keep track of current salary and previous threesalaries
05 SALARY-DATA OCCURS 4 TIMES.10 SALARY PIC 9(6)V99.10 SAL-DATE PIC 9(4).
Figure 11.4 OCCURS Clause at the Group Level
SALARY-DATA (2)
SALARY (2) SAL-DATE (2)
SALARY-DATA (1)
SALARY (1) SAL-DATE (1)
SALARY-DATA (3)
SALARY (3) SAL-DATE (3)
(a) COBOL Statements
(b) Storage Schematic
05 SALARY-DATA. 10 SALARY OCCURS 4 TIMES PIC 9(6)V99.10 SAL-DATE OCCURS 4 TIMES PIC 9(4).
Figure 11.5 OCCURS Clause at the Elementary Level
SALARY (2) SAL-DATE (2)
SALARY-DATA
SALARY (1) SAL-DATE (1) SAL-DATE (3)SALARY (3)
(b) Storage Schematic
(a) COBOL Statements
SUBSCRIPTS
Defined in WORKING-STORAGEUSAGE COMP - internal
representation binarySyntax
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 usedRuntime error if subscript 0 or
greater than table size
SALARY REPORT FORXXXXXXXXXXXXXXXXXXXXXXXXXXXX
CURRENT 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
Report Specifications
(b) Computation of Percent Salary Increase
PERFORM VARYING SUB FROM 1 BY 1UNTIL SUB > 3 OR SALARY (SUB + 1) = 0 COMPUTE PCT-SALARY-INC (SUB)
= 100 * ((SALARY (SUB) - SALARY (SUB+1)) / SALARY (SUB + 1)
END-COMPUTEEND-PERFORM
Figure 11.6 Relative Subscripting
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 TIMESDEPENDING 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.7 Variable-length Records
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
300-W RIT ET RANS
HEADING
410-INCREM ENTCOUNT ERS
420-W RIT EDET AIL
LINE
310-PROCESSCOURSES
320-W RIT EG PA
330-ADD T ODEANS
LIST
200-CREAT ET RANSCRIPT
340-W RIT EDEANS-LIST
HEADING
350-W RIT EDEANS-LIST
DET AILS
210-W RIT EDEANS
LIST
100-PROCESSST UDENTRECORDS
Figure 11.10 Hierarchy Chart for Transcript Program
Figure 11.11 Pseudocode for Transcript Program
Open filesDO WHILE data remains READ Student file AT END
Indicate no more data NOT AT END
Write transcript headingMove zero to quality-point and credit countersDO for each course Determine multiplier for this course Increment total quality points Increment total credit counter Write detail line for this courseENDDOCOMPUTE grade-point-average = total quality points /’ total creditsWrite grade-point-averageIF dean’s list IF students on dean’s list > 100
Display appropriate error message ELSE
Increment students on dean’s listMove this student to dean’s list table
END-IFEND-IF
ENDREADENDDOWrite heading for dean’s listDO for every student on dean’s list Write student dataENDDOClose filesStop run
INDEXES
Defined as part of the OCCURS clauseMore efficient than subscripts
OCCURS integer-1 TIMES [ASCENDING/DESCENDING KEY IS data-name][INDEXED BY index-name-1]
Used the same as a SubscriptRequires no other definitionMust use SET verb
Table 11.1 Indexes versus Subscripts
INDEXES SUBSCRIPTSDefined 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