Page 1 of 1

Need help on trimming cobol string

PostPosted: Wed Jul 19, 2017 3:36 pm
by aftermath09
Hi,

Need help on trimming cobol string using INSPECT or PERFORM.

I have a string in this format with delimeters: (01)SSN(02)MobileNumber(03)DateApplied(04)DateApproved

The only required field is the SSN, others can be blank. So, if I only have SSN and Date Applied, string would be (01)SSN(02)...........(03)DateApplied(04) ............

Note: The (......) represents the spaces.

What I need is to trim the fields with blank value so that I will have something like this: 01)SSN(03)DateApplied.

TIA!

Re: Need help on trimming cobol string

PostPosted: Wed Jul 19, 2017 4:45 pm
by Robert Sample
MOVE 1 TO OUTPUT-LOC
PERFORM
    VARYING INPUT-LOC FROM 1 BY 1
      UNTIL INPUT-LOC > LENGTH OF <input string>
    IF  <input string> (INPUT-LOC : 1) NE SPACE
        MOVE <input string> (INPUT-LOC : 1)
          TO <output-string> (OUTPUT-LOC : 1)
        ADD 1 TO OUTPUT-LOC
    END-IF
END-PERFORM
This is untested code but the process is straightforward. Use reference modification to check each byte of the input string; if it is not a space move it to the right spot in the output string and bump the output location up by 1. You'll need to change the <> values to the actual variable names in your program.

Oh, a terminology note for future reference: COBOL does not have strings that behave like in Java or C; COBOL has variables that are not the same.

Re: Need help on trimming cobol string

PostPosted: Wed Jul 26, 2017 9:49 pm
by lawnless
You don't indicate whether you need to carry all the delimiters over to your output file. If you have an input file of
(01)SSN| |(03)DateApplied|
do you need an output layout of
(01)SSN||(03)DateApplied|