- The STRING verb in COBOL is used for combining strings which is called STRING concatenation. In this program, we're reading a sequential file that contains a list of employees with their Last Name, First Name, and Middle Initial. Using the STRING command we will format the name to be displayed as First Name, followed by a space, Middle Initial, followed by a period, and Last Name. To eliminate any extra spaces in the username we will also use the optional delimited by space, to allow us to get only the name and not all the extra spaces allowed for longer names.
Let's update this program to format the employee name. We start by updating the detail print line to have only one variable for the employee name. I'm going to scroll down to my working storage section and I'm going to change my WS DETAIL LINE to remove all the lines associated with the individual parts of F Name, Middle, and L Name. And I'm going to change WS DETAIL NAME to be defined as an alphanumeric field that contains 23 characters.
This allows room for someone with a First Name of 10 characters, a Last Name of 10 characters, a Middle Initial, a period, and a space between First Name and Middle Initial. Next, in the process employee paragraph, we need to remove the lines from moving the individual names and add our STRING command. As you can see, I'm leaving the commands to inspect the SSN and add hyphens so that our social security number will have the three digits, a hyphen, two digits, a hyphen, and then the last four digits.
Now what I'm going to do is I'm going to remove these three lines, 70 to 72, because I don't need them anymore, and I'm going to add my STRING command. As you can see, I'm also adding delimited by size so that it will shrink the field down to only the characters that are inside the field. And I'm going to place the entire thing into WS DET NAME. Next, we display the detail line and then I actually added another display to get a blank line between each person.
Let's save this, compile it and run the program to see what it looks like. I'll start by compiling it. Ok, let me scroll up a little bit. It looks like the student list is printing out but it's odd that several people have additional letters added on to their Last Name. It seems that I forgot to initialize the WS DETAIL NAME before I actually merged the fields together. Let's go back to our program and correct this error. So in line 68, I need to add a statement and say move spaces to WS DETAIL NAME.
That way I won't have any extra letters from any names that were longer than the current name I'm working with. I'm going to save it again. I'm going to recompile my program. Now, I'm going to run it again. Ah! That looks much better. Ok. Let me scroll to the top. Here's our student list. We have the names on the left, Danielle F. Corkery, and the SSN on the right with the hyphens. As you can see, all the names have the First Name with a space, the Middle Initial with a period, and the Last Name, with no extra spaces between.
We accomplished this using the STRING verb. The STRING verb can be used in many different ways. I hope this example helps you to try and think of other ways to use it on your own.
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