- Tables are extremely useful in COBOL. A table is a collection of data items that have the same description, such as account totals, or monthly averages. The table consists of a table name, and subordinate items, called table elements. You might want to think of a table as the COBOL equivalent to an array in other languages. I have a sample program here, that will read in a file that contains a city name and 12 months' worth of rain fall totals for each city.
Each rainfall amount is a three digit number, with an implied decimal point. My program is in the left-hand side, and my sample file is on the right-hand side. You can see I have rainfall totals for Houston, State College, Boston, San Francisco, Cleveland, and Baltimore. To read each record, I use a file description that includes the city name, and I'm going to use a table for the rainfall amounts. The file definition starts here on line 13. On line 18, is where I'm using a table to hold all of the rainfall totals by month.
You can see I have RAIN-AMOUNT, defined as a pic clause, 99V9, which is an implied decimal, occurs 12 times. On a side note, it is important to make sure your statements do not enter columns 73 or higher. That is why I often use multiple lines for one statement. For example, I have the OCCURS 12 TIMES, below the rain amount statement. If your code accidentally goes into column 73 or higher, it will be ignored by the compiler, and this can cause syntax errors.
And I have to tell you, even worse, you get a very misleading error message. Hopefully you don't have to find out the hard way, like I did. Okay, back to tables. By using the occurs statement, I avoided creating a separate variable for each of the 12 months. The advantages of this approach include the code clearly shows the unity of the items, of the table elements, you can use subscripts, or indexes, to refer to individual items in the table, you can easily repeat data items.
Tables are important for increasing the speed of a program, especially one that looks up records. Now that we've seen where I defined the rainfall totals, let's scroll down to line 101, where we process each record in the file. As you can see, I start by moving the CITY-NAME to the detail city name. Then I had a PERFORM. I'm perform varying, a variable, WS-MONTH, from one by one, until the month is equal to 13.
Once it gets to 13, it will have already processed one through 12. Inside of my perform statement, I moved the rain amount and I used that WS-MONTH variable as my subscript, or my index value. So the first time through, it'll move January's amounts from the rain amount value in the file to the detail rain amount value in my detail record. I end the perform, then I display the detail line. For this program, I also use a table in my detail line.
Again, so I don't have to repeat the variable 12 times. Let's scroll up a little bit and look at that. On line 77 is my detail line, I print out the city, followed by the rain amount. On line 79, I have WS-DET-AMT, DET-amount, occurs 12 times. I have a filler of two spaces, and then I use the decimal point, I do 99.9, so that when it prints the rainfall total, it'll actually put a decimal point in there for me.
So this is an example of a program that reads in data from a file, puts the data into a table, and then I actually use another table in order to print the data back out again.
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