- I'd like to just go over my solution to this problem. As you can see, on the right, I have the customer's .csv file open, which is my comma delimited file. And on the left, I have my sample code solution. Let's walk through the code. On line seven, we define our customer's file, and on line 10, we define the output file for our mailing. Let's scroll down a little bit. We have to define, in the Data Division, both files. The customer file can have up to 61 characters.
The mailing line will only have, at most, 26 characters across one single line. Because we're going to break up the name, the street, and the city into three separate lines. In working storage, I define each of the variables that we find in our file. Last name, first name, house number, street, city, state and zip code. Lines 42 to 44 I'll use in order to write data out to my mailing file. Let's scroll down to the Procedure Division.
Line 49 is where the first paragraph begins. The first thing I do inside the paragraph is open up my customer's file for input and my mailing file for output. I read the first record, then I go to 200 process records. Let's scroll down so we can see the whole paragraph. In process records, I have to start by finding the last position of any data in the string. I'm using a perform statement, perform varing string end from 61 by minus one, until I get to a point where there's no more spaces.
Next, I use the unstring command to unstring the details delimited by a comma into the field you see listed there. The last name, first name, etc. Line 77 to 79, I move spaces to my output fields to make sure that none of the data from the previous record is left behind. I'm going to scroll down a little bit. Now that I've used the unstring command to break my file up into pieces, now I want to use the string command to be able to format my mailing label.
On line 81, I string the first name, along with the last name into the ws name line. On line 83, I take the house number, along with the street name into the address line. In line 85, I take the city, with a comma, along with the state and the zip code into the city line. Then I write three records using these fields. Then I read the next customer record, and repeat the process. At the bottom, I close my customer's file, and my mailing file.
Let's run the program so you can see how it works. I've already compiled the program, so I just need to run it. 08_ 05_ solution It looks like it ran. We won't know until we open up our mailing file. So let's go back over to Notepad++, and let's open it up. File. Open. I'm going to scroll down until I find the mailing.lpt There it is. Mailing.lpt You'll notice that it got rid of any extra spaces.
So the first mailing address is Peggy Fisher 123 Main Street Reedsville, PA 17094 Well, it looks like all my unstring and string commands worked the way I expected them to. I hope you had fun with this last challenge, and were able to come up with your own solution. This has been a lot of fun learning COBOL with you.
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