- Tables are extremely helpful when you have multiple occurrences of data with the same format. For example, if we wanted to read a file of quarterly sales for popular video games, we could read each record, and save it in a table. Then, we can traverse the table to add up total sales by quarter, or even search the table for the highest sales game in quarter one. As you can see, on the right, I have a video sales file that has a SKU number, a video game title, and quarterly sales.
Our program will read the file, store the data in a table, and allow us to display the information in a report. To save time, I've already coded most of the program, and it's available in the Exercise files folder for Chapter 7, Section 02. But, I didn't create the table yet. So, let's go ahead, and create the table that we'll use to hold our information. As you can see in the program on the left, I'm reading in the video game file, I'm defining the video game file, starting on Line 13.
On Line 16, I define the data values. The first four numbers are the SKU number, followed by the title, followed by four occurrences of the quarterly sales. I'm going to scroll down a little bit, so we can see more of the working-storage section. On Line 30, in the working-storage, you can see, I've added two subscript values. One is called "Game Table subscript," and one is just called "subscript." Below, that, I want to add my table to hold my game info. Let's go ahead, and add that next.
I'll start by creating a 05 element label, called "Game Info." The game info, according to my table, occurs ten times. Next, I define the table values. The first value is going to be the table SKU. And it has a PIC clause of four numbers. I'll do nine, parentheses four parentheses. Next, I have the title. So, we're going to call this "table-game-title." The title can be up to 20 characters long.
So, I'll use "x," "20." The next field is actually the quarterly sales. So, we'll do table, dash, I'll spell out the word "quarterly" "-sales." I'll make this a group item, because we have four occurrences of the sales. So, below this, I'll add another field called table q-t-r-l-y sales, and it's going to have a PIC value of five numbers, five digits, and it occurs four times.
So, I'm actually creating a table within a table. When accessing our table quarterly sales, because it is a table within a table, we'll need to provide two subscripts. The first one will identify the element in the Game Info table. The second subscript, we use to identify which element of the four quarterly sales I'm referring to. Okay, the remainder of the working-storage is my definition for my report. So, I have a title, I have a heading line, a detail line, and a total line, which is not being used, just yet.
But it's there for future reference. Okay, here's the procedure division. Of course, we always open our input file, and then we read the first record. If it's end of file, we set that to "true." I'm going to display my report title, and my two heading lines, and on Line 95, I'm setting my Game table subscript to one. This is a little different than tables in, say, Java or C++, 'cause those subscripts usually start with "0". But, in COBOL, we start with "1." Next, I perform 200 process records.
Paragraph 200 starts on Line 100. First thing I do, is move my SKU value from my incoming file to my table SKU value, using my Game table subscript. I do the same thing for the title. Notice, on Line 104, when I move the sales, I have to give it two values. I have to tell it where in the Game table to store the value, and I'm going to store the first quarterly sales in the first position. And then, you'll see I do quarterly sales two, quarterly sales three, and quarterly sales four.
For the four quarters. At the end, don't forget to update your subscripts, so that you don't overlay your previous record in your table On Line 113, I add one to my game table subscript. I read the next record until its end of file. Let's scroll down a little bit more, and you can see, in the Display Details paragraph, again, I use a perform varying subscript from one, by one, until subscript is greater than ten, I move the information from the table, to the detail line, and then I display the detail line.
Let's run the program, and see what it looks like. Okay. I already have it compiled, so let's just go ahead and run it, 07, 02, create, table. let me move it down a little bit, so you can see the file on the right, and compare it to the information in the report. It says the SKU number is 1-2-3 for Halo, which is correct, and then the quarterly sales, $1,200, $1,000, $950, and sales always go up in quarter four, due to holidays.
All right, all ten of my video games are showing there. So, it looks like my logic is working, and my table was created correctly. I do want to point out, that it's also possible to create a variable-length table, using the depending-on phrase of the occurs clause. When creating tables, you can arrange them in ascending or descending order, based on the values in one or more key fields of the table, just by coding the word "ascending" or "descending" in the occurs clause. Tables are widely used in all programming languages, so it is worth taking some time to practice using tables in COBOL.
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