Question

I want to make three fields into one field with only a single space between each word in Cobol. I this the correct format below

  STRING SORT-WORKER-LAST  SPACE       
         SORT-WORKER-FIRST SPACE       
         SORT-WORKER-MID   SPACE       
       DELIMITED BY SIZE               
         INTO REC-VSAM-NAME      

This didn't work:

   STRING SORT-WORKER-LAST  SPACE       
          SORT-WORKER-FIRST SPACE       
          SORT-WORKER-MID   SPACE       
       DELIMITED BY space              
         INTO REC-VSAM-NAME        

   STRING SORT-WORKER-LAST         
          SORT-WORKER-FIRST        
          SORT-WORKER-MID          
       DELIMITED BY space              
         INTO REC-VSAM-NAME     

Not working either.

      SS5726    test                test           t

     " " DELIMETED BY SPACE

This above code is not giving me what I am looking for either.

Was it helpful?

Solution

When used in a STRING statement, the figurative constant SPACE (or SPACES, they are equivalent, the plural means nothing except for human reading) has a length of one byte.

You may not be finished with this. If your source fields contain embedded spaces, you will be best to abandon STRING and do something else.

If you proceed with STRING or there is another time you want to consider using it, then you also have to think about the length of your output field. If you don't do anything about it, it will be quietly truncated.

I've included an example of how to do something. Note that the STRING now has a conditional element (ON), so you must delimit the scope of the STRING by END-STRING (also possible, but tacky, with full-stop/period).

If, logically, the output cannot be breached, the ON OVERFLOW is not needed. Also, if what you are told to do is "just truncate" then it can be omitted, although I'd tend to at least count them, and display the count at the end of the program. Then when the Analyst has said, "there won't be any, just truncate if there are" you can go back and say that there were 3,931 when you did your volume test.

As ScottNelson has pointed out in a comment, there are a couple of things to watch out for with STRING. What concerns you here is that only the data selected by the STRING will appear in your output field, your output field will not be space-padded, as it would be after a MOVE statement.

Because you have been using fixed-length fields up to now, you won't have noticed this. Once you have the correction, you may find, if you are not setting the output field to SPACE first, that you have a mixture of values, with some left over from the previous content.

Another one with STRING is the POINTER.

The effects of the way STRING works is useful if that is what you want. You just have to know what to do to avoid those things when you don't want that action.

Every time you find something new in COBOL, hit that manual. Language Reference first. Try to understand. Programming Guide. Try further. If unsure, experiment. Read manual. Experiment. Continue until understood.

Each time I read the manual, I try to look at something else as well. One technique with knowing a language is to know the type of thing that can be done, and to know where to find the detail, and how to understand the explanations.

You will find similar things with all the "complex" COBOL verbs, STRING, UNSTRING, INSPECT. They have actions which seem initially to be working against you, but which are useful, and otherwise not available, when you need them.

   IDENTIFICATION DIVISION. 
   PROGRAM-ID. DOUGH. 
   DATA DIVISION. 
   WORKING-STORAGE SECTION. 
   01  PART-1 PIC X(30) VALUE "TEST". 
   01  PART-2 PIC X(30) VALUE "TEST". 
   01  PART-3 PIC X(30) VALUE "T". 
   01  ALL-PARTS PIC X(30). 
   PROCEDURE DIVISION. 
       MOVE SPACE TO ALL-PARTS
  *    MOVE ZERO TO data-name-used-with-POINTER
  *    (if used)                                 
       STRING PART-1 DELIMITED BY SPACE 
              SPACE DELIMITED BY SIZE 
              PART-2 DELIMITED BY SPACE 
              SPACE DELIMITED BY SIZE 
              PART-3 DELIMITED BY SPACE 
         INTO ALL-PARTS 
         ON OVERFLOW 
           DISPLAY "SORRY, YOUR DATA WAS TRUNCATED"
       END-STRING 

       DISPLAY 
             ">" 
                ALL-PARTS 
             "<" 
       GOBACK 
       . 

OTHER TIPS

Try....

STRING SORT-WORKER-LAST DELIMITED BY SPACE
       " " DELIMITED BY SIZE
       SORT-WORKER-FIRST DELIMITED BY SPACE
       " " DELIMITED BY SIZE
       SORT-WORKER-MID DELIMITED BY SPACE
  INTO REC-VSAM-NAME

Try

STRING
    field-1 DELIMITED BY SIZE
    " " DELIMITED BY SIZE
    field-2 DELIMITED BY SIZE
  INTO big-field

Just for completeness sake, you can do the following if you want to be able to cope with data fields that have embedded spaces (in other words, text fields containing multiple words):

INSPECT SORT-WORKER-FIRST
        REPLACING TRAILING SPACES BY LOW-VALUES.
INSPECT SORT-WORKER-MID
        REPLACING TRAILING SPACES BY LOW-VALUES.
INSPECT SORT-WORKER-LAST
        REPLACING TRAILING SPACES BY LOW-VALUES.
STRING SORT-WORKER-LAST " " SORT-WORKER-FIRST " " SORT-WORKER-MID
       DELIMITED BY LOW-VALUE INTO REC-VSAM-NAME.

For instance, this would cope when SORT-WORKER-LAST contained something like "VAN DYKE".

If you didn't want to modify the existing SORT-WORKER-* fields, you'd have to move each to a separate field and INSPECT and then STRING those fields.

What you are doing here is converting each of the strings to the 'C' equivalent - terminated by a NUL.

Of course, this depends if your Cobol is new enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top