Table Lookups - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Table Lookups

Description:

05 FILLER PIC X(14) VALUE 02ART HISTORY'. 05 FILLER PIC X(14) VALUE 04BIOLOGY' ... 01 END-OF-MAJOR-FILE-SW PIC X VALUE SPACES. 88 END-OF-MAJOR-FILE VALUE Y' ... – PowerPoint PPT presentation

Number of Views:201
Avg rating:3.0/5.0
Slides: 33
Provided by: Robe395
Category:
Tags: lookups | pic | table

less

Transcript and Presenter's Notes

Title: Table Lookups


1
Chapter 12
  • Table Lookups

2
CODES
  • Stored in files to represent more complex values
  • Save on Storage
  • Provide consistency
  • Fewer Error
  • Easier to verify

3
Figure 12.1 Table of Major Codes
02 ART HISTORY04 BIOLOGY19 CHEMESTRY21 CIVIL
ENGINEERING24 COMP INF SYS32 ECONOMICS39 FINANC
E43 MANAGEMENT49 MARKETING54 STATISTICS
4
TABLE 12.1 Types of Table Codes
CODE TYPE USED 1 POSITION 2
POSITIONS n POSITIONS
Numeric 0 - 9 101 10 102 100
10n Alphabetic A - Z 261 26 262 676
26n Alphanumeric A - Z, 0 - 9 361 36
362 1,296 36n
Characteristics of Codes
  • Precise - each code is unique allowing only
    one alternative
  • Mnemonic - easy to remember meaning of the
    code
  • Expandable - allows for future growth

5
Table Characteristic
  • Internal VS External
  • Internal - Hard coded in Working Storage
  • External
  • A File that needs to be read into program
  • May change frequently
  • Easy to maintain

6
Table Characteristic (cont)
  • Argument - Value Tables
  • Argument and Value are stored together in table
    Argument Value 02
    Art History 04
    Biology
  • Ordered vs. Unordered
  • Positional / Direct Access Tables
  • No argument stored in table
  • Argument is always an integer value Jan
    Feb Mar

7
Table Characteristic (cont)
  • Range Step Tables
  • Argument is a range of valuesFed Tax tables are
    range step tables Income
    Tax 0 - 600 0.0
    601 - 2400 15.25
  • Parallel Tables
  • Value found in searching one table provides data
    needed to find a value in a second table

8
Sequential Look Up
  • Table made up of Table Argument and Table Value
  • 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
  • Takes N/2 tries

9
Figure 12.2 Sequential Table Lookup
39 - Find the Major description that corresponds
to a Major Code 39
1st try 2nd try 3rd try 4th try 5th try 6th
try 7th try
We can use Perform Varying to accomplishCan you
write the statement?
Match
FINANCE
PERFORM VARYING SUB FROM 1 BY 1 UNTIL INPUT-CODE
TABLE-CODE OR SUB 10
On average will take 5 tries to find a match N/2
- 10/2
10
Binary Look Up
  • Must be ordered - By argument
  • 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
  • Takes N tries where 2N Size of the table

11
Figure 12.3 Binary Lookup
39
1st try
Match
3rd try
FINANCE
2nd try
12
TABLE 12.2 Required Number of Comparisons for
Binary Search
Number of Table Elements
Maximum Number of Searches
8 - 15 16 - 31 32 - 63 64 - 127 128 - 255 256 -
511 512 - 1023 1024 - 2047 2048 - 4095
(less than 24) (less than 25) (less than
26) (less than 27) (less than 28) (less than
29) (less than 210) (less than 211) (less than
212)
4 5 6 7 8 9 10 11 12
13
Figure 12.4 Positional Organization Direct
Lookup
02
ART HISTORY
04
BIOLOGY
. . .
19
CHEMESTRY
39
21
CIVIL ENGINEERING
24
COMP INF SYS
. . .
32
ECONOMICS
. . .
1st try
Match
39
FINANCE
FINANCE
. . .
43
MANAGEMENT
. . .
14
Redefines
  • Assigns another name / format to a previously
    defined data item
  • Used in Data division
  • Data items must be same level
  • Redefinition must occur immediately following the
    data item to be redefined
  • Must account for the exact amount of storage area

15
Figure 12.5 Initializating and Internal Table
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. 10 MAJOR-CODE PIC 9(2). 10
EXPANDED-MAJOR PIC X(12).
16
Figure 12.6 Table Initialization (Storage
Schematic)
17
Figure 12.7 Input-Loaded Table
FD MAJOR-CODE-FILE. RECORD CONTAINS 14
CHARACTERS DATA RECORD IS MAJOR-CODE-RECORD.0
1 MAJOR-CODE-RECORD. 05 INCOMING-FILE-CODE P
IC 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 PI
C 99 VALUE ZERO. 01 END-OF-MAJOR-FILE-SW PIC
X VALUE SPACES. 88 END-OF-MAJOR-FILE VALUE
Y.
(a) Data Division entries
18
Figure 12.7 Input-Loaded Table
PROCEDURE DIVISION.OPEN INPUT MAJOR-CODE-FILE.PE
RFORM 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
SMALLEND-IF.CLOSE MAJOR-CODE-FILE PERFORM
0100-PREPARE-STUDENT-TRANSCRIPT. . .
(b) In-Line Perform
19
Figure 12.8 Input-Loaded Tables
19CHEMISTRY
04BIOLOGY
02ART HISTORY
02
ART HISTORY
04
BIOLOGY
19
CHEMISTRY
EXPANDED-MAJOR (1)
EXPANDED-MAJOR (2)
EXPANDED-MAJOR (3)
MAJOR-CODE (1)
MAJOR-CODE (2)
MAJOR-CODE (3)
20
Figure 12.9 Sequential Lookup with PERFORM VARYING
WORKING-STORAGE SECTION.01 TABLE-PROCESSING-ELEM
ENTS. 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).
21
Figure 12.9 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.
22
Search Verb
SEARCH implements a sequential table lookup
easier to use than PERFORM VARYING SERACH
identifier-1 VARYING index-name-1 AT END
imperatives WHEN condition-1 imperatives
WHEN condition-2 imperatives END-SEARCH
23
Figure 12.10 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
24
Figure 12.10 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
25
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
26
Search All Restrictions
  • WHEN clause can only use
  • Only one WHEN clause
  • VARYING may not be used
  • Table Argument 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
    Table Argument
  • Must be ordered by the Key

27
Figure 12.11 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)
28
Figure 12.11 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.
29
Table 12.3 SEARCH versus SEARCH ALL
SEARCH SEARCH ALL
  • 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

30
Figure 12.12 Direct Access
31
Figure 12.13 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
32
Figure 12.13 Range-step Table
01 SCHOLARSHIP-TABLE. 05 GPA-SCHOLARSHIP-PERCE
NTAGES. 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
Write a Comment
User Comments (0)
About PowerShow.com