Presentation on theme: "Chapter 6 Structured Data 1. A data structure, or record, in COBOL is a method of combining several variables into one larger variable. – Example: 2."— Presentation transcript:
Chapter 6 Structured Data 1
A data structure, or record, in COBOL is a method of combining several variables into one larger variable. – Example: 2
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). 3
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 4
Structured Data 5
Filler and How to use it? 6
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. 7
Filler and How to use it? 8
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. 9
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. 10
Calculating length of Data Structure 11
Calculating length of Data Structure A structure variable is treated as an alphanumeric variable. It has an implied PICTURE of X(nn), where nn is equal to the length of the structure variable. THE-WHOLE-MESSAGE has an implicit PICTURE of X(61). 12
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. 13
Calculating length of Data Structure 14
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. 15
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 16
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. 17
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. 18
Nested Structure Variables Any structure can contain another structure. The indention makes it clear that these variables are subordinate to THE-MESSAGE. 19
Nested Structured Variables 20 Refer to coding 1. To calculate the length of a structure variable containing one or more other structure variables.
Misusing Structures 21 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 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 77 23
Level 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 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":
Level 88 26
Level 88 27
Level 88 28
Level 88 29
Level 88 30
Level 88 31
Q&A 32 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 33 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 34 1.What is the length of THE-WHOLE-MESSAGE in the following example?
Quiz 35 2.What is the implied PICTURE of THE-WHOLE- MESSAGE in question 1? 3.What is a data structure? 4.If you move a value to a structure variable, what happens to the values in the individual variables within the structure? 5.In the following code, what is another way of performing the test at line ?
Quiz 36 6.In the following code, what is another way of performing the test at line ?
Quiz 37 7.In the following code, what is another way of performing the test at lines and ?
Quiz 38 8.Devise a level 88 condition name for YES-NO that would simplify the tests at lines through in the following code: