- In COBOL, we use picture clauses to describe the nature of incoming or outgoing data. A picture clause that uses all 9's means the entry is numeric. A picture clause that uses x's, means that the entry is alphanumeric. Which includes letters, numbers and special characters. Data types are defined as part of the file section to describe the fields of data in a record. A record is a set of fields, and a file is a set of records.
Additional data fields are needed for temporary storage, and are used for calculations. Other data fields represent switches to control the execution of paragraphs, and sometimes the program needs to hold constant values or values that do not change. In this sample program, under the file section, we can see that some fields are defined with pic clauses using 9's to represent numeric data, and others are using x's to represent alphanumeric data.
For example if I scroll down, we can see on line 27 the salesperson ID is a numeric ID and it has five numbers. That's why it says pic 9(5). I could also write that as five 9's. Let me show you. So pic 9 (5) is the same as pic 9999. below the file descriptions, we have working storage. This is where we define any additional variables that we might need.
In this case the ws-total-sales in working storage is a field that I'm going to use to add the yearly sales for each sales associate and in the end print out the total for the year. Working storage is also used to define the record layout for any reports you might want to create. Let me scroll down so we can see what I'm talking about. On line 48 I have a heading line. This line contains all the information that would be the heading on a report.
Below the heading line is the detail line which will be used to print out each salesperson separately along with their region and their yearly sales. And finally I have a total line, which will print out the total yearly sales. It is sometimes helpful to use a print layout chart to create your report so you know exactly how many filler spaces you need between each of the fields. Speaking of filler, did you notice the filler entries for the report record definitions? For example, lines 49 to 55 all use filler.
You can tell that filler is a keyword because it's highlighted in blue. A filler entry defines a field that is not referenced anywhere else in the COBOL program. But is used as a placeholder. This concept of using filler is quite different than many of the more recent programming languages. But it comes in handy when you're creating a report using COBOL. In addition to defining the data names and the picture clauses for each size, each variable can also have a value clause. Such as value spaces, value zeros, or even some initial values.
The value clause initializes the contents of a data name within the working storage section. As you can see here I have on line 49 a value clause for spaces. On line 50 I'm actually providing a value of salesperson name. That's the heading of the salesperson column. We also need to address that some variables contain a sign. It may be positive or negative. Signed variables in COBOL have a pic clause that includes an S at the start of the variable size.
Let me scroll down a little bit and you can see what I mean. On line 77 I have working storage WS-NUM1 which has a pic clause of S9(3)V99. The S means that NUM1 could be positive or negative. What do you think the V means in that particular pic clause? If you said decimal, you're absolutely right. The V represents a decimal. And P is an assumed decimal.
On line 80 I have an example of an alphanumeric field for the name where I have pic A(6) and on line 81 I have a pic X clause, to show you that it can contain numbers, letters, and special symbols. To start a new data name, you must specify the level number. This is usually 01 for the field description entry and group level items. 02-49 are elementary items.
Level number 66 is reserved for the rename clause. Level 77 is for items that cannot be subdivided. And finally level 88 is for condition name entries. Notice that most of the examples here are either starting with 01 or 05. 01 is considered a group item, which might have 1 or more elementary items associated with the group item. Let me scroll up to my file definition. Here on line 25 I have a group item 01 SALESDETAILS.
Below that I have an 88 level which is an ENDOFSALES VALUE which is getting set to HIGH-VALUES. And I'll use that to indicate the end of file. Below that is my first elementary item which is the salesperson ID. Below the salesperson ID in line 28 is the salesperson's name. Notice there's no size associated with the name, because that's a group item, and below that the two elementary items make up the last name and the first name.
So remember when you're working in COBOL if you want to define a numeric variable, you want to use your pic clause of 9's and if you're working with alphanumeric data you want to use a pic clause including the x's.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings