330 likes | 609 Views
Tables and Table Processing (con’t). SUBSCRIPTS. All tablearray elements require Subscript or Index (defined later) Subscripts always defined by programmer USAGE COMP - efficiency Syntax DATE (2) or DATE (Month-No) Relative Subscripting BONUS (YEARS + 2). CAVEATS.
E N D
SUBSCRIPTS • All table\array elements require Subscript or Index (defined later) • Subscripts always defined by programmer • USAGE COMP - efficiency • 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
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
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 PERFORM ADD are used instead); can also be manipulated in statements PERFORM statements Provide more efficient object code than USAGE IS COMPUTATIONAL makes subscriptssubscripts more efficient, although indexes are still faster Must be used with the SEARCH verb Can be used to identify items in more than one table/array
Internal vs External Ordered vs Unordered Argument - Function Tables Positional Tables / Direct Access Tables Range Step Tables Parallel Tables Table Characteristic
Sequential Look Up • Table made up of Table Argument and Table Function • May or may not be Ordered • Always starts at element 1 • Compares input value with Table Argument • Proceed thru the table item by item until a matching value is found or table end
Binary Look Up • Must be ordered • Starts in the middle of the table • Compares Value for equal • If not equal determines which half of the table the value is in • Continues cutting table into Halves until value is found
Input-Loaded Table FD MAJOR-CODE-FILE. RECORD CONTAINS 14 CHARACTERS DATA RECORD IS MAJOR-CODE-RECORD.01 MAJOR-CODE-RECORD. 05 INCOMING-FILE-CODE PIC 9(2). 05 INCOMING-FILE-NAME PIC X(12).. . .WORKING-STORAGE SECTION.01 MAJOR-TABLE. 05 MAJORS OCCURS 1 TO 10 TIMES DEPENDING ON NUMBER-OF-MAJORS INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12). 01 NUMBER-OF-MAJORS PIC 99 VALUE ZERO. 01 END-OF-MAJOR-FILE-SW PIC X VALUE SPACES. 88 END-OF-MAJOR-FILE VALUE ‘Y’. (a) Data Division entries
Input-Loaded Table PROCEDURE DIVISION.200-Housekeeping. … PERFORM 300-Load-Table 300-Load-Table. OPEN INPUT MAJOR-CODE-FILE.PERFORM VARYING MAJOR-INDEX FROM 1 BY 1 UNTIL MAJOR-INDEX > 10 OR END-OF-MAJOR-FILE READ MAJOR-CODE-FILE AT END MOVE ‘Y’ TO END-OF-MAJOR-FILE NOT AT END ADD 1 TO NUMBER-OF-MAJORS MOVE INCOMING-FILE-CODE TO MAJOR-CODE (MAJOR-INDEX) MOVE INCOMING-FILE-NAME TO EXPANDED-MAJOR (MAJOR-INDEX) END-READEND-PERFORM.IF MAJOR-INDEX > 10 DISPLAY ‘MAJOR TABLE TOO SMALL’END-IF.CLOSE MAJOR-CODE-FILE . (b) In-Line Perform
Sequential Lookup with PERFORM VARYING WORKING-STORAGE SECTION.01 TABLE-PROCESSING-ELEMENTS. 05 WS-MAJOR-SUB PIC S9(4) COMP. 05 WS-FOUND-MAJOR-SWITCH PIC X(3) VALUE ‘NO’. 05 WS-END-OF-TABLE-SWITCH PIC X(3) VALUE ‘NO’. 01 MAJOR-VALUE. 05 FILLER PIC X(10) VALUE ‘02ART HISTORY’. 05 FILLER PIC X(10) VALUE ‘04BIOLOGY’. 05 FILLER PIC X(10) VALUE ‘19CHEMISTRY’. 05 FILLER PIC X(10) VALUE ‘21CIVIL ENG’. 05 FILLER PIC X(10) VALUE ‘24COMP INF SYS’. 05 FILLER PIC X(10) VALUE ‘32ECONOMICS’. 05 FILLER PIC X(10) VALUE ‘39FINANCE’. 05 FILLER PIC X(10) VALUE ‘43MANAGEMENT’. 05 FILLER PIC X(10) VALUE ‘40MARKETING’. 05 FILLER PIC X(10) VALUE ‘54STATISTICS’. 01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12).
Sequential Lookup with PERFORM VARYING PROCEDURE DIVISION. 0000-PREPARE-STUDENT-REPORT. MOVE ‘NO’ TO WS-FOUND-SWITCH WS-END-OF-TABLE-SWITCH. PERFORM 1000-FIND-MAJOR VARYING WS-MAJOR-SUB FROM 1 BY 1 UNTIL WS-END-OF-TABLE-SWITCH = ‘YES’ OR WS-FOUND-MAJOR-SWITCH = ‘YES’. . . .1000-FIND-MAJOR. IF WS-MAJOR-SUB > 10 MOVE ‘YES’ TO WS-END-OF-TABLE-SWITCH MOVE ‘UNKNOWN’ TO HDG-MAJOR ELSE IF ST-MAJ0R-CODE = MAJOR-CODE (WS-MAJOR-SUB) MOVE ‘YES’ TO WS-FOUND-MAJOR-SWITCH MOVE EXP-MAJOR (WS-MAJOR-SUB) TO HDG-MAJOR END-IF END-IF.
Search Verb SEARCH implements a sequential table lookup easier to use than PERFORM … VARYING SEARCH identifier-1 [VARYING [index-name-1] ] [ AT END imperatives] [WHEN condition-1 imperatives] [WHEN condition-2 imperatives] END-SEARCH
SEARCH Statement (Sequential Lookup) 01 MAJOR-VALUE. 05 FILLER PIC X(14) VALUE ‘02ART HISTORY’. 05 FILLER PIC X(14) VALUE ‘04BIOLOGY’. 05 FILLER PIC X(14) VALUE ‘19CHEMISTRY’. 05 FILLER PIC X(14) VALUE ‘21CIVIL ENG’. 05 FILLER PIC X(14) VALUE ‘24COMP INF SYS’. 05 FILLER PIC X(14) VALUE ‘32ECONOMICS’. 05 FILLER PIC X(14) VALUE ‘39FINANCE’. 05 FILLER PIC X(14) VALUE ‘43MANAGEMENT’. 05 FILLER PIC X(14) VALUE ‘40MARKETING’. 05 FILLER PIC X(14) VALUE ‘54STATISTICS’.01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12).. . . INDEXED BY clause required in table definition
SEARCH Statement (Sequential Lookup) PROCEDURE DIVISION.. . .SET MAJOR-INDEX TO 1.SEARCH MAJORS AT END MOVE ‘UNKNOWN’ TO HDG-MAJOR WHEN ST-MAJOR-CODE = MAJOR-CODE (MAJOR-INDEX) MOVE EXP-MAJOR (MAJOR-INDEX) TO HDG-MAJOREND-SEARCH. SET statement establishes starting point
Search All Verb SEARCH ALL implements a binary table lookup easier to use than PERFORM … VARYING SERACH ALL identifier-1 [ AT END imperatives] [WHEN condition-1 imperatives] END-SEARCH
Search All Restrictions • WHEN clause can only use = • Only one WHEN clause • VARYING may not be used • Occurs item and its index appears to left of = signVALID WHEN S-AMT (X1) = AMTINVALID WHEN AMT = S-AMT (X1) • Requires table to be defined using the KEY clause
SEARCH ALL Statement (Binary Lookup) 01 MAJOR-VALUE. 05 FILLER PIC X(14) VALUE ‘02ART HISTORY’. 05 FILLER PIC X(14) VALUE ‘04BIOLOGY’. 05 FILLER PIC X(14) VALUE ‘19CHEMISTRY’. 05 FILLER PIC X(14) VALUE ‘21CIVIL ENG’. 05 FILLER PIC X(14) VALUE ‘24COMP INF SYS’. 05 FILLER PIC X(14) VALUE ‘32ECONOMICS’. 05 FILLER PIC X(14) VALUE ‘39FINANCE’. 05 FILLER PIC X(14) VALUE ‘43MANAGEMENT’. 05 FILLER PIC X(14) VALUE ‘40MARKETING’. 05 FILLER PIC X(14) VALUE ‘54STATISTICS’.01 MAJOR-TABLE REDEFINES MAJOR-VALUE. 05 MAJORS OCCURS 10 TIMES ASCENDING KEY IS MAJOR-CODE INDEXED BY MAJOR-INDEX. 10 MAJOR-CODE PIC 9(2). 10 EXPANDED-MAJOR PIC X(12).. . . KEY required for binary search (ASCENDING or DESCENDING)
SEARCH ALL Statement (Binary Lookup) PROCEDURE DIVISION.. . .SEARCH ALL MAJORS AT END MOVE ‘UNKNOWN’ TO HDG-MAJOR WHEN MAJOR-CODE (MAJOR-INDEX) = ST-MAJOR-CODE MOVE EXP-MAJOR (MAJOR-INDEX) TO HDG-MAJOREND-SEARCH.
Implements a sequential lookup Requires a SET statement prior to SEARCH to establish the initial position in the table Does not require codes in the table to be in any special sequence Contains an optional VARYING clause (see Figure 13.18) May specify more than one WHEN clause Implements a binary lookup Does not require an initial SET statement (calculates its own starting position Requires the codes to be in ascending or descending sequence on the associated KEY clause in the table definition Does not contain a VARYING clause Restricted to a single WHEN clause SEARCH versus SEARCH ALL SEARCH SEARCH ALL
Range-step Table Grade Point Scholarship Average Percentage 3.75 - 4.00 100 3.50 - 3.74 75 3.25 - 3.49 50 3.00 - 3.24 33 2.75 - 2.99 25 2.50 - 2.74 15 (a) Scholarship Table
Range-step Table 01 SCHOLARSHIP-TABLE. 05 GPA-SCHOLARSHIP-PERCENTAGES. 10 FILLER PIC X(6) VALUE ‘375100’. 10 FILLER PIC X(6) VALUE ‘350075’. 10 FILLER PIC X(6) VALUE ‘325050’. 10 FILLER PIC X(6) VALUE ‘300033’. 10 FILLER PIC X(6) VALUE ‘275025’. 10 FILLER PIC X(6) VALUE ‘250015’. 05 GPA-TABLE REDEFINES GPA-SCHOLARSHIP-PERCENTAGES OCCURS 6 TIMES INDEXED BY GPA-INDEX. 10 GPA-MINIMUM PIC 9V99. 10 SCHOLARSHIP-PCT PIC 999.. . . SET GPA-INDEX TO 1. SEARCH GPA-TABLE AT END MOVE ZERO TO SCHOLARSHIP-AWARD WHEN STUDENT-GPA >= GPA-MINIMUM (GPA-INDEX) MOVE SCHOLARSHIP-PCT (GPA-INDEX) TO SCHOLARSHIP-AWARD END-SEARCH. (b) COBOL Implementation
Responsibility 1 26,000 2 27,000 3 28,000 4 30,000 5 32,000 6 34,000 Responsibility level = 4 7 36,000 8 39,000 9 42,000 10 46,000 Multilevel Tables (a) One-Level Table
Responsibility level = 1Experience level = 4 Responsibility level = 4Experience level = 1 Experience 1 2 3 4 5 Responsibility 1 26,000 27,000 28,000 29,000 30,000 2 27,000 28,000 29,000 30,000 31,000 3 28,000 29,000 30,000 31,000 32,000 4 30,000 32,000 34,000 36,000 38,000 5 32,000 34,000 36,000 38,000 40,000 6 34,000 36,000 38,000 40,000 42,000 7 36,000 39,000 42,000 45,000 48,000 8 39,000 42,000 45,000 48,000 51,000 9 42,000 45,000 48,000 51,000 54,000 10 46,000 50,000 54,000 58,000 62,000 Multilevel Tables
Experience Experience 1 1 2 2 3 3 4 4 5 5 Responsibility Responsibility 1 1 26,000 28,000 29,000 27,000 28,000 30,000 29,000 31,000 30,000 32,000 2 2 29,000 27,000 28,000 30,000 29,000 31,000 32,000 30,000 33,000 31,000 3 3 28,000 30,000 29,000 31,000 32,000 30,000 33,000 31,000 32,000 34,000 4 4 30,000 32,000 34,000 32,000 34,000 36,000 36,000 38,000 40,000 38,000 5 5 32,000 32,000 34,000 34,000 36,000 36,000 38,000 38,000 42,000 40,000 6 6 34,000 34,000 36,000 36,000 38,000 38,000 40,000 40,000 42,000 44,000 7 7 36,000 36,000 39,000 39,000 42,000 42,000 45,000 45,000 50000 48,000 8 8 39,000 39,000 42,000 42,000 45,000 45,000 48,000 48,000 51,000 53,000 9 9 42,000 42,000 45,000 45,000 48,000 48,000 51,000 51,000 56,000 54,000 10 10 46,000 46,000 50,000 50,000 54,000 54,000 58,000 58,000 64,000 62,000 Multilevel Tables Region = 2Responsibility level = 4Experience level = 1 Region = 2Responsibility level = 4Experience level = 1 Region 2 Region = 1Responsibility level = 4Experience level = 1 Region 1 Region = 1Responsibility level = 1Experience level = 4
Two-Level Table 01 SALARY-VALUES. 05 FILLER PIC X(5) VALUE ‘2600027000280002900030000’. 05 FILLER PIC X(5) VALUE ‘2700028000290003000031000’. 05 FILLER PIC X(5) VALUE ‘2800029000300003100032000’. 05 FILLER PIC X(5) VALUE ‘3000032000340003600038000’. 05 FILLER PIC X(5) VALUE ‘3200034000360003800040000’. 05 FILLER PIC X(5) VALUE ‘3400036000380004000042000’. 05 FILLER PIC X(5) VALUE ‘3600039000420004500048000’. 05 FILLER PIC X(5) VALUE ‘3900042000450004800051000’. 05 FILLER PIC X(5) VALUE ‘4200045000480005100054000’. 05 FILLER PIC X(5) VALUE ‘4600050000540005800062000’. 01 SALARY-TABLE REDEFINES SALARY-VALUES. 05 RESPONSIBILITY OCCURS 10 TIMES. 10 EXPERIENCE OCCURS 5 TIMES. 15 SALARY PIC 9(5). (b) Initialization via the REDEFINES and VALUES Clauses
PERFORM VARYING with Two Subscripts PERFORM VARYING RESPONSIBILITY-SUB FROM 1 BY 1 UNTIL RESPONSIBILITY-SUB > 10 AFTER EXPERIENCE-SUB FROM 1 BY 1 UNTIL EXPERIENCE-SUB > 5 MOVE ZERO TO SALARY (RESPONSIBILITY-SUB, EXPERIENCE-SUB)END-PERFORM (b) In-Line Perform
Three-Level Tables 01 SALARY-VALUES. 03 REGION-ONE. 05 FILLER PIC X(5) VALUE ‘2600027000280002900030000’. 05 FILLER PIC X(5) VALUE ‘2700028000290003000031000’. 05 FILLER PIC X(5) VALUE ‘2800029000300003100032000’. 05 FILLER PIC X(5) VALUE ‘3000032000340003600038000’. 05 FILLER PIC X(5) VALUE ‘3200034000360003800040000’. 05 FILLER PIC X(5) VALUE ‘3400036000380004000042000’. 05 FILLER PIC X(5) VALUE ‘3600039000420004500048000’. 05 FILLER PIC X(5) VALUE ‘3900042000450004800051000’. 05 FILLER PIC X(5) VALUE ‘4200045000480005100054000’. 05 FILLER PIC X(5) VALUE ‘4600050000540005800062000’. 03 REGION-TWO. 05 FILLER PIC X(5) VALUE ‘2800029000300003100032000’. 05 FILLER PIC X(5) VALUE ‘2900030000310003200033000’. 05 FILLER PIC X(5) VALUE ‘3000031000320003300034000’. 05 FILLER PIC X(5) VALUE ‘3200034000360003800040000’. 05 FILLER PIC X(5) VALUE ‘3400036000380004000042000’. 05 FILLER PIC X(5) VALUE ‘3600038000400004200044000’. 05 FILLER PIC X(5) VALUE ‘3800041000440004700050000’. 05 FILLER PIC X(5) VALUE ‘4100044000470005000053000’. 05 FILLER PIC X(5) VALUE ‘4400047000500005300056000’. 05 FILLER PIC X(5) VALUE ‘4800052000560006000064000’. 01 SALARY-TABLE REDEFINES SALARY-VALUES. 05 REGION OCCURS 2 TIMES. 10 RESPONSIBILITY OCCURS 10 TIMES. 15 EXPERIENCE OCCURS 5 TIMES. 20 SALARY PIC 9(5). (b) Initialization via the REDEFINES and VALUES Clauses
PERFORM VARYING with Three Subscripts PERFORM VARYING REGION-SUB FROM 1 BY 1 UNTIL REGION-SUB > 2 AFTER RESPONSIBILITY-SUB FROM 1 BY 1 UNTIL RESPONSIBILITY-SUB > 10 AFTER EXPERIENCE-SUB FROM 1 BY 1 UNTIL EXPERIENCE-SUB > 5 MOVE ZERO TO SALARY (REGION-SUB, RESPONSIBILITY-SUB, EXPERIENCE-SUB)END-PERFORM. (b) In-Line Perform
Branch Name San Diego Atlanta Boston Chicago $50,000 1st Quarter $25,000 $100,000 $150,000 $50,000 $165,000 $150,000 2nd Quarter $200,000 $400,000 $250,000 $300,000 3rd Quarter $75,000 $350,000 4th Quarter $400,000 $100,000 $275,000 Two-Level Table Lookup . . . (a) User’s View 01 SALES-TABLE REDEFINES SALES-DATA. 05 BRANCH OCCURS 25 TIMES INDEXED BY BRANCH-INDEX. 10 BRANCH-NAME PIC X(12). 10 QUARTERLY-SALES OCCURS 4 TIMES INDEXED BY QUARTERLY-INDEX PIC 9(6). (b) Table Definition
continued MOVE ZEROS TO ANNUAL=TOTAL.SET BRANCH-INDEX TO 1.SEARCH BRANCH AT END DISPLAY ‘Boston not in table’ WHEN BRANCH-NAME (BRANCH-INDEX) = ‘Boston’ PERFORM VARYING QUARTERLY-INDEX FROM 1 BY 1 UNTIL QUARTERLY-INDEX > 4 ADD QUARTERLY-SALES (BRANCH-INDEX, QUARTERLY-INDEX) TO ANNUAL-TOTAL SIZE ERROR DISPLAY ‘ANNUAL TOTAL TOO LARGE’ END-ADD END-PERFORMEND-SEARCH (d) SEARCH Statement