380 likes | 518 Views
Chapter 6. Structured Data. Structured Data. A data structure, or record, in COBOL is a method of combining several variables into one larger variable. Example:. Structured Data. THE-WHOLE-MESSAGE is a structure variable or simply a structure.
E N D
Chapter 6 Structured Data
Structured Data • A data structure, or record, in COBOL is a method of combining several variables into one larger variable. • Example:
Structured Data • THE-WHOLE-MESSAGE is a structure variable or simply a structure. • It is occasionally referred to as a compound variable or compound data. • In a structure variable, the highest-level variable (the one that includes all the individual variables) has the level number 01. • The 01 level must appear in Area A (columns 8 through 12). • The structure variable name appears in Area B (columns 12 through 72), and it does not have a PICTURE. • The variables that fall within the structure begin with numbers higher than 01, and start in Area B (columns 12 through 72).
Structured Data • You can use the individual variables within a structure in the program as though they still were level 01 variables. • In addition, you can use the structure variable as a variable. • For example, if all the variables within a structure variable can be displayed, the structure variable itself can be displayed as a variable
Filler and How to use it? • They are used to format part of the display and are assigned values in the definition, but nothing is ever moved to these values in the program. • They exist only to fill out THE-WHOLE-MESSAGE. • this type of value in a structure variable can be defined as a filler by using the COBOL reserved word FILLER.
Filler and How to use it? • A FILLER cannot be treated as a variable. • It is used to reserve space in a structure variable. • You can assign a PICTURE and a VALUE to a FILLER when it is defined, but you cannot use MOVE with FILLER.
Calculating length of Data Structure • A data structure is actually a series of individual variables, laid end to end in memory. • The length of a simple data structure, such as this one (see example at next slide) used to create a displayable message, is the sum of all the lengths of the individual parts.
Calculating length of Data Structure • A structure variable is treated as an alphanumeric variable. It has an implied PICTURE of X(nn), where nnis equal to the length of the structure variable. • THE-WHOLE-MESSAGE has an implicit PICTURE of X(61).
Calculating length of Data Structure • You can move a value to a structure variable, but the move will affect the entire length of the variable. • A structure variable and the variables that are the elements of a structure occupy the same memory area. • When a variable is created by the compiler, it sets aside a number of bytes in memory that can be used to hold data.
Calculating length of Data Structure • Compare figure 8.2 and figure 8.3 • The two fillers, as well as EMP-NUMBER and EMP-HOURLY, occupy some bytes that are in the same space in memory as the structure variable EMPLOYEE-DATA. • When you use a command in COBOL to modify a variable in memory, the command looks at variables in memory as individual units. • If you move a value to EMP-NUMBER, or use ADD 1 TO • EMP-NUMBER, COBOL acts on EMP-NUMBER as if it were a single variable and ignores the fact that EMP-NUMBER is part of the structure EMPLOYEE-DATA.
Calculating length of Data Structure • If you move a message to EMPLOYEE-DATA, the command treats EMPLOYEE-DATA as if it were a PIC X(20) (the implied picture) and ignores the fact that EMPLOYEE-DATA has smaller variables within it. • Example
Calculating length of Data Structure • The variables in EMPLOYEE-DATA do not disappear, but the MOVE affects all 20 bytes of memory. • The individual variables might no longer contain data that is correct for that variable type. • From the figure: • EMP-NUMBER now contains ore, which certainly is not valid numeric data. • This isn't a problem as long as you don't use a command on EMP-NUMBER, such as ADD 1 TO EMP-NUMBER. I'll return to this issue in a moment.
Calculating length of Data Structure • This use of a structure variable is fairly common in display and print programs that might use a structure to format and display information line by line, and then at the end of the program might move a message to the entire structure and display it.
Nested Structure Variables • Any structure can contain another structure. • The indention makes it clear that these variables are subordinate to THE-MESSAGE.
Nested Structured Variables • Refer to coding 1. • To calculate the length of a structure variable containing one or more other structure variables.
Misusing Structures • It is common to find programs that define data structures in WORKING-STORAGE that never are used as structures. • Grouping variables together under a structure variable because they are similar or to keep things tidy isn't a good practice. • It is better to use comments in WORKING-STORAGE to separate groups of variables used for different purposes.
Level 77 • When you see data in a structure in a program, you assume that the structure is used somewhere in the program as a structure, and you can be confused if it is not. • A variable that is not a structure can be given a level number of 77 instead of 01: • For examples , see next slide
Level 88 • Level 88 is a special level number used to improve the readability of COBOL programs and to improve IF tests. • A level 88 looks like a level under another variable, • It does not have a PICTURE, • but it does have a value.
Level 88 • A level 88 is always associated with another variable and is a condition name for that variable. • Both of the following conditions test whether YES-NO is equal to "Y":
Q&A • Q What happens if I move to a FILLER? • A You can't. The compiler does not recognize FILLER as a variable name; it is used only to reserve space in a structure variable. • If you include a command such as MOVE 1 TO FILLER in a program, it will not compile and produces an error. • You can move values to a structure variable that contains a FILLER. • The MOVE will affect the structure variable and all of the variables within the structure, but you cannot directly move values to a FILLER.
Q&A • Q Why do I need to know the lengths of structures? • A You don't yet, but you will need to know how to calculate this when you start creating printed reports (in Day 10, "Printing").
Quiz • What is the length of THE-WHOLE-MESSAGE in the following example?
Quiz • What is the implied PICTURE of THE-WHOLE-MESSAGE in question 1? • What is a data structure? • If you move a value to a structure variable, what happens to the values in the individual variables within the structure? • In the following code, what is another way of performing the test at line 004600?
Quiz • In the following code, what is another way of performing the test at line 004600?
Quiz • In the following code, what is another way of performing the test at lines 004600 and 004700?
Quiz • Devise a level 88 condition name for YES-NO that would simplify the tests at lines 004600 through 004900 in the following code: