IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Removing Leading Trailing Spaces in a field


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Fri Jun 28, 2013 10:42 am
Reply with quote

Hi,

I have to generate a CSV file which should not have any leading and traling spaces in any of the fields.

All the fields may have leading spaces, trailing spaces and a combination of both. Like
Code:
WS-STRING1 could be  'TEST STRING    ' , '      TEST STRING' or '      TEST STRING    '

I'm using the following logic to remove leading and trailing spaces (which I found on this forum..thanks for that.).

Code:
INITIALIZE WS-HIGH-STRING.                                 

MOVE       FUNCTION REVERSE(WS-STRING1)                     
                          TO WS-HIGH-STRING                 

INSPECT    WS-HIGH-STRING REPLACING                         
                          LEADING SPACES BY X'FF'           

MOVE       FUNCTION REVERSE(WS-HIGH-STRING) TO WS-STRING1. 
                                                           
INSPECT    WS-STRING1     TALLYING WS-SPACE-COUNT           
                          FOR LEADING SPACES               

COMPUTE    WS-OFFSET1 = WS-SPACE-COUNT + 1                   

COMPUTE    WS-LENGTH1 = LENGTH OF WS-STRING1 - WS-SPACE-COUNT


But I have around 40 fields in my ouput files.
So currently I'm performing the above for all of them and then

Code:
MOVE 1 TO PTR-FIELD                   
STRING WS-STRING1(WS-OFFSET1:WS-LENGTH1)
            DELIMITED BY X'FF'         
       ','                             
            DELIMITED BY SIZE         
       WS-STRING2(WS-OFFSET2:WS-LENGTH2)
            DELIMITED BY X'FF'         
       ','                             
            DELIMITED BY SIZE   
........................................
........................................
........................................
........................................       
INTO WS-OUTREC
       WITH POINTER PTR-FIELD
END-STRING.                 
 



So I just wanted to know if I'm doing it correctly and is there any other efficient way of doing it.

Thanks.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Fri Jun 28, 2013 12:10 pm
Reply with quote

I think OpenCOBOL has FUNCTION TRIM. Doesn't help you much, as we don't :-)

Since you are already using "reference-modification" for the "input" fields, you could avoid the use of STRING and use reference-modification to build your output.

You could do everything in a "loop2, so that you don't have 40 sets of fields.

You could use OCCURS DEPENDING ON. At least one example if you search here.

You could your byte-per-byte, with a "flip-flop" flag which tells you, flipped by each delimiter, flopped by non-blank, remembering that the start of the data is also a delimiter, and that the length of the data is useful to know.

If you have "embedded delimiters", delimiters which can be valid in the data, your STRING solution will not work (the reference-modification/ODO would still work, as they are only concerned with lengths, not content).
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Jun 28, 2013 4:30 pm
Reply with quote

If you have 40 variables to deal with, write a subprogram that accepts a variable and the length of the variable, returning the number of leading spaces and number of trailing spaces. This will keep you from having 39 repetitions of code in your main program.
Back to top
View user's profile Send private message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Fri Jun 28, 2013 6:30 pm
Reply with quote

Thanks Bill and Robert..

Quote:
If you have "embedded delimiters"

No..we won't have this situation of embedded delimiters..Online program which are updating the VSAM, which is source to this CSV file, is having those validations to convert comma into spaces..before writing into the VSAM.

What I'm thinking is rather than passing the subprogram one variable ..I will pass the entire record..I mean my subprogram will receive Fixed format, Comma delimited Input record and return the record after removing all the leading and trailing spaces between two delimiters.

Like Input record passed will be
Code:
'   THIS , IS , A , TEST STRING    '


and Subprogram will return:
Code:
'THIS,IS,A,TEST STRING'


Is it a good idea?
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Jun 29, 2013 10:56 pm
Reply with quote

Code:

03  WS-STRING PIC X(256).
03  WS-WORK-STRING PIC X(256).
03  WS-SUB PIC 9(08) COMP.

MOVE '   THIS , IS , A , TEST STRING    ' TO WS-STRING.
MOVE ZERO TO WS-SUB.
MOVE 1 TO TALLY.
MOVE SPACES TO WS-WORK-STRING.

PERFORM UNTIL TALLY > LENGTH OF WS-STRING
      IF   WS-STRING (TALLY:1) NOT = SPACE
            ADD 1 TO WS-SUB
            MOVE WS-STRING (TALLY:1) TO WS-WORK-STRING (WS-SUB:1)
      END-IF
      ADD 1 TO TALLY
END-PERFORM.

MOVE WS-WORK-STRING TO WS-STRING.

Adjust the picture-clause size of WS-STRING and WS-WORK-STRING accordingly.

HTH....
Back to top
View user's profile Send private message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Sat Jun 29, 2013 11:09 pm
Reply with quote

Hi Bill..

But won't it also replace the intermediate space apart from Leading and Trailing..
In case of
Code:
'   THIS , IS , A , TEST STRING    '

It will return
Code:
'THIS,IS,A,TESTSTRING'

rather than
Code:
'THIS,IS,A,TEST STRING'
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Sun Jun 30, 2013 1:41 am
Reply with quote

In your example, you have three "types" of space. "Leading", "embedded" and "trailing".

Leading space and trailing space, you want to remove.

Embedded space you want to keep.

With Mr Bill's code, the leading space is easy. So the question is "how to identify embedded space and trailing space as separate things".

When you get to the next non-blank (or end of data) if you found a comma (or end of data) that was trailing space that just went by, so get rid of it, if it is something else, it is embedded, so leave it alone.
Back to top
View user's profile Send private message
mistah kurtz

Active User


Joined: 28 Jan 2012
Posts: 316
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Mon Jul 01, 2013 11:38 am
Reply with quote

Thank you all...i have got it working.

I have put a check for next as well as previous "non-blank" character in the input string and if it's not the delimiter(",") than move it into output string.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Keep leading zero(s) after convert fl... SYNCSORT 7
No new posts Remove leading zeroes SYNCSORT 4
No new posts leading spaces can be removed in trai... DFSORT/ICETOOL 1
No new posts Join 2 files according to one key field. JCL & VSAM 3
Search our Forums:

Back to Top