Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Removing Leading Trailing Spaces in a field

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
mistah kurtz

Active User


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

PostPosted: Fri Jun 28, 2013 10:42 am    Post subject: Removing Leading Trailing Spaces in a field
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

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7225

PostPosted: Fri Jun 28, 2013 12:10 pm    Post subject: Reply to: Removing Leading Trailing Spaces in a field
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: 7905
Location: Bellevue, IA

PostPosted: Fri Jun 28, 2013 4:30 pm    Post subject:
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: 268
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Fri Jun 28, 2013 6:30 pm    Post subject:
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: 2502
Location: Atlanta, Georgia, USA

PostPosted: Sat Jun 29, 2013 10:56 pm    Post subject: Reply to: Removing Leading Trailing Spaces in a field
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: 268
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Sat Jun 29, 2013 11:09 pm    Post subject:
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

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7225

PostPosted: Sun Jun 30, 2013 1:41 am    Post subject: Reply to: Removing Leading Trailing Spaces in a field
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: 268
Location: Room: TREE(3). Hilbert's Hotel

PostPosted: Mon Jul 01, 2013 11:38 am    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Add PD field from 2nd file to PD in 1st Sushant Garje DFSORT/ICETOOL 6 Thu Dec 01, 2016 4:32 pm
No new posts How to split the records using the am... vnktrrd DFSORT/ICETOOL 24 Fri Oct 28, 2016 7:33 pm
No new posts Sort records based on numeric field. Alks SYNCSORT 2 Wed Oct 19, 2016 10:14 pm
No new posts Amount field is getting corrupted whe... thesumitk SYNCSORT 5 Tue Oct 18, 2016 8:20 pm
No new posts To trim spaces in a pipe delimited da... Nish84 COBOL Programming 16 Mon Oct 10, 2016 1:54 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us