- In Chapter 5, we talked about a lot of advance sequential file manipulation. In this challenge, we had to take two input files, merge and sort them, and then print out a final report of all employees. Well it turns out that the merge and sort was probably the easiest part. But closing the file and reopening it for printing might have been a little tricky. Let's walk through my solution. In my file, starting on line seven in my file control, I declare Acme Employees, and I assign that to the incoming Acme data, as you can see on the right.
Next, on line 11, I select the employees from Fuses Incorporated, and there's the data for them. On line 14, I identify the file that will contain the new, sorted, and merged file, and I'm calling that EMPLOYEES.NEW. On line 17, any time you use a sort or merge verb in your program, you need to assign a workfile. So I'm assigning workfile to WORK.TMP. Let's scroll down the program a little bit so we can see all of the data division.
Inside here, we have our file description for Acme employees and Fuses employees. I said earlier that we've already gone ahead and dealt with making them the same size files, and made sure all the fields lined up between the two companies. On line 28 is the definition of our sorted file, and then on line 38, is our sort description for our workfile. Notice I have one field that has a variable name, WF-SSM. That's because the program's going to use that field to sort on.
If I had a secondary key, I would have to specify that here as well. Let's continue down to the procedure division. On line 71 is my first paragraph, READ-EMPLOYEES. I open my two files as input. On line 75, this is where all the work's being done and it says MERGE WORKFILE ON ASCENDING KEY, workfile-SSN, using the two files, Acme employees and Fuses Incorporated employees. GIVING SORTEDFILE.
That's it. That one statement can actually cause a long run time. But since our files are so small, it won't be difficult for us. Now, I want to be able to print a report of the merged file. So on line 81, I open the sorted file as input this time. I read the first record in the sorted file, I display the heading for the report, and then I execute the paragraph 250 to print the new employees. Here, I just move the information from the input file to my detail line, and then display my detail line.
When I'm done, I close the sorted file and end the program. Let's run it and take a look at the report and the file. You can see the header, EMPLOYEE ID, EMPLOYEE NAME, and SALARY, and you can see, it lists all the employees, starting with good old Elmer Fudd all the way down to Bugs Bunny. If you look on the far left, you'll notice they're sorted by social security number. Okay, it looks like that worked. Let's go ahead and check our file.
Okay, I want to open up EMPLOYEES.NEW. It looks like all the records are there as well. As a reminder, don't worry if your code looks quite different than mine. Everybody programs a little differently, but I hope that this code helped you to understand a little bit more about merging, sorting, and actually being able to open and close files as different file types, whether they're input or output.
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