- The unstring verb is used to divide a string into substrings. This is the opposite of the string command. Have you ever worked with a comma-delimited file? This type of file is a great candidate for the unstring verb Comma-delimited files contain variable length data that is separated by commas. In this example, I'm using a comma-delimited file as input and then using the unstring command to parse the data and print the information in a readable report. On the right is my sample file.
We define the file with the max value for the length of each record. On the left, you'll see in the data division, I define the comma-delimited file with a value of details with a size of 110 characters. In the working storage section, I need to have fields defined to hold each of the individual fields in the file. This starts on line 24. The first field is going to be my short description, followed by a movie name, followed by an issue number, a time code, and the last value is the decision.
To make the program more efficient, I've added logic to find the end of the data in each record before using the unstring command. So on line 30, I had to add a variable called STRINGEND. You'll see in the file on the right, each record is a different size. Let's scroll down a little. Starting on line 38 is my detail line, where I'll print out the information with some spaces between each field. Let's scroll down to the process records paragraph. Here, you can see I added a perform statement on line 67 to vary the field string end from 110 by -1 until it finds a value that's not equal to a space.
That gives me the exact length of the record. Using that value, in line 71, in my unstring command, I unstring the details record, starting in position one, going until the end of the values for this record. I look for any field that's delimited by a comma and I parse it into these variables: into short description, movie name, issue number, time code, and decision. That's the end of the unstring.
From there, we can move each field into the detail line and finally, I display the detail line. This process continues until the comma-delimited file hits end of file, and then I close the file and end the program. Let's take a look at the report and compare it to our comma-delimited file here. Okay, let me scroll up a little bit. As you can see, the title is "COMMA DELIMITED FILE EXAMPLE." If you look at the file on the right, you'll see that the first comma comes after No Action On Screen.
And that's the first value in my report. Then there's a space, User-defined methods, another space, a number, a time code, and a decision to fix it. If we scroll down through, we can see that each one of these records was read and parsed into its parts and then printed out in a report. The unstring command is a great tool for dealing with comma-delimited files.
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