110 likes | 204 Views
TABLES (chp. 12). group of storage locations (i.e. elements) with the same name accessed by name and subscript ex. var-name (sub) similar to arrays in other languages. OCCURS Clause. Defines the number of elements in the table for each level Defines the maximum subscript for each level
E N D
TABLES (chp. 12) • group of storage locations (i.e. elements) with the same name • accessed by name and subscript ex. var-name (sub) • similar to arrays in other languages Tables
OCCURS Clause • Defines the number of elements in the table for each level • Defines the maximum subscript for each level • Up to seven levels of OCCURS permitted in COBOL • Can not be used on 01 level item SUBSCRIPTS • Integer values from 1 to occurs clause value (specifies maximum value) • Up to 7 subscripts are permitted in COBOL • Subscripts are listed in parenthesis following the table name • Each time the value of a subscript changes, data-name (sub) refers to a different element in the array • Using a data-name as a subscript enables its contents to be varied • Relative subscripting is used when what is in parenthesis can be an arithmetic expression • if using subscript constants (named or literal) compiler will detect error • if using subscript variables, error won't be detected until run-time Tables
TABLE examples • If item defined by OCCURS has PIC clause, it defines a series of elementary items • 01 Totals. • 05 Month-Totals Occurs 12 Times Pic 9(5)V99. For your information When a variable name has an OCCURS clause on it or above it with a smaller level number, it must have one subscript when referencing that memory location for each OCCURS. Tables
TABLE examples • Identifier used with OCCURS may also be group item • 01 Tax-Table. • 05 Group-X Occurs 20 Times. • 10 City Pic X(6). • 10 Tax-Rate Pic V999. Tables
More TABLE examples EXAMPLE #1 01 DAYS-OF-MONTH-TABLE. 03 MONTHS OCCURS 12 times. 05 Month-Name PIC X(15). 05 Month-Num-Days PIC 99. Move “January” to Month-Name (1) Move 31 to Month-Num-Days (1) EXAMPLE #2 01 DAYS-OF-MONTH-TABLE. 05 Month-Name PIC X(15) OCCURS 12 TIMES. 05 Month-Num-Days PIC 99 OCCURS 12 TIMES. Move “January” to Month-Name (1) Move 31 to Month-Num-Days (1) How else initialize Month-Name and Month-Num-Days?! Tables
One More TABLE example 01 GRADE-TABLE. 05 QUIZ-TABLE OCCURS 40 TIMES. 10 QUIZ1 PIC 9(2). 10 QUIZ2 PIC 9(2). 10 QUIZ3 PIC 9(2). 10 QUIZ4 PIC 9(2). How do we average all the quiz scores for QUIZ4? Compute-Average-Quiz4. MOVE ZERO TO Total-Grade MOVE 1 TO Sub PERFORM UNTIL Sub > Num-Quizzes ADD QUIZ4(Sub) TO Total-Grade ADD 1 TO Sub END-PERFORM DIVIDE Total-Grade BY Num-Quizzes GIVING Average-Score ***Convert to Perform-Varying Tables
INITIALIZING TABLES Two ways to use VALUE clause to initialize all elements to zero: 01 Array-1 Value Zero. 05 Totals Occurs 50 Times Pic 9(5). 01 Array-1. 05 Totals Occurs 50 Times Pic 9(5) Value Zero. Can also initialize each element to different value 01 Day-Names Value 'SUNMONTUEWEDTHUFRISAT'. 05 Days Occurs 7 Times Pic X(3). ??? Can an initialization be done on the 05 directly above??? INITIALIZE STATEMENT (i.e. in Procedure Division) A series of elementary items contained within a group item can all be initialized. Numeric items will be initialized to zero and nonnumeric items will be initialized to blank. FORMAT: INITIALIZE {identifier-1} … Tables
REDEFINES Purpose: Used to define a field with a value clause then redefine it as an array. However, you cannot do the reverse; that is, once an entry has been defined by an OCCURS clause, it may NOT be redefined. FORMAT in Data Division: L# identifier-1 REDEFINES identifier-2 [finish declaration] where: L# means level number Identifier-1 and identifier-2 must be on the same level EXAMPLES: See table example program 01 Rates. 05 10Rate PIC 99V999 value 12.345. 05 100Rate REDEFINES 10Rate PIC 999V99. 05 1000Rate REDEFINES 10Rate PIC 9999V9. Result 100Rate = 123.45 and 1000Rate = 1234.5 01 HoldDate. 02 EuroDate. 03 EuroDay PIC 99 value 11. 03 EuroMonth PIC 99 value 3. 03 EuroYear PIC 9(4) value 2003. 02 USDate REDEFINES EuroDate. 03 USMonth PIC 99. 03 USDay PIC 99. 03 USYear PIC 9(4). USMonth = 3 and USDay = 11 Tables
2 dimensional TABLES (ex#1) Define array as follows: 01 Temperature-Array. 05 Day-In-Week Occurs 7 Times. 10 Hour Occurs 24 Times. 15 Temp Pic 9(3). For your information: If reading in from file, what does input file look like?! FD input file 01 input-rec pic 999. Find the average temp for: each day. Find the average temp for each hour. PARTICIPATION POINTS (5) (due in one week – graded on if works!) What if have negative temperatures? How solve?! That is, what does input file look like? What does declaration look like? Perform a from 1 by 1 until a > 7 perform b from 1 by 1 until b > 24 read input-file at end.. move input-rec to temp (a b) end-perform End-perform Tables
2 dimensional TABLES (ex#2) 01 Jeans-Table. 05 Jean-Gender OCCURS 2 TIMES. 10 Jean-Color OCCURS 3 TIMES. 15 Jean-SalesValue PIC 9(8)V99. 15 Jean-NumSold PIC 9(7). Tables
2 dimensional TABLES (ex#3) 01 Jeans-Table. 05 Jean-Size OCCURS 10 TIMES. 10 Exact-Size PIC 99. 10 Jean-Style PIC X. 10 Jean-Color OCCURS 3 TIMES. 15 Jean-SalesValue PIC 9(8)V99. 15 Jean-NumSold PIC 9(7). Tables