Need help on trimming cobol string



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Need help on trimming cobol string

Postby aftermath09 » Wed Jul 19, 2017 3:36 pm

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!
aftermath09
 
Posts: 1
Joined: Wed May 30, 2012 8:02 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Need help on trimming cobol string

 

Re: Need help on trimming cobol string

Postby Robert Sample » Wed Jul 19, 2017 4:45 pm

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.
Robert Sample
Global moderator
 
Posts: 3419
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 235 times

Re: Need help on trimming cobol string

Postby lawnless » Wed Jul 26, 2017 9:49 pm

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|
lawnless
 
Posts: 5
Joined: Tue Nov 10, 2015 11:23 pm
Has thanked: 0 time
Been thanked: 1 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post