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

Remove trailing spaces.


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

New User


Joined: 14 Mar 2008
Posts: 74
Location: India,Chennai

PostPosted: Fri Sep 05, 2008 5:35 pm
Reply with quote

Hi All,
I have a requirement in which i have to remove all the trailing spaces from an variable. Can any one provide me a solution on how this can be acheived.
Example:
Consider the value to be 'abcd efgh ' the output should be 'abcd efgh'. All the spaces in the trailer should be removed.
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 Sep 05, 2008 6:20 pm
Reply with quote

Uh ... you do know that COBOL variables are fixed length, right? Just exactly what do you want to replace the space with? You've got to replace it with another character -- LOW-VALUES, HIGH-VALUES, printable character, non-printing character.
Back to top
View user's profile Send private message
ssk1711

New User


Joined: 16 Jun 2008
Posts: 40
Location: bangalore

PostPosted: Fri Sep 05, 2008 6:25 pm
Reply with quote

Hi

I think you can use VARCHAR.

Please specify exact requirement, with an example.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Sep 05, 2008 6:35 pm
Reply with quote

ssk1711 wrote:
Hi

I think you can use VARCHAR.

Please specify exact requirement, with an example.


VARCHAR in COBOL?
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 Sep 05, 2008 6:36 pm
Reply with quote

Look in the COBOL Language Reference and you will not find any mention of VARCHAR, which is a database field type. VARCHAR fields in DB2 become fixed-length COBOL variables when the data is moved.
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: Fri Sep 05, 2008 7:41 pm
Reply with quote

Proceed backwards until you find the first position which is greater than space -

Code:

           03  WS-STRING           PIC  X(256).                           
           03  WS-SUB              PIC S9(008)     BINARY.               
      *                                                                   
           MOVE 'ABCD EFH'             TO WS-STRING.                     
           MOVE LENGTH OF WS-STRING    TO TALLY.                         
           MOVE ZERO                   TO WS-SUB.                         
      *                                                                   
           PERFORM UNTIL TALLY < 1                                       
               IF  WS-STRING (TALLY:1) > SPACE                           
                   MOVE TALLY          TO WS-SUB                         
                   MOVE ZERO           TO TALLY                           
               ELSE                                                       
                   SUBTRACT 1          FROM TALLY                         
               END-IF                                                     
           END-PERFORM.                                                   

After falling thru from the PERFORM, if WS-SUB is non-zero (in this case, 08), then this is the length of the string, before the start of trailing spaces.

Optionally, INSPECT FUNCTION REVERSE can also be used, but that's up to you and requires at a minimum, COBOL/370.

Bill
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Sep 05, 2008 9:05 pm
Reply with quote

You will extract the data and again will put in a variable of length 256 only right ? so what abt the places after the data ? it will again be spaces ..

As Robert asked the trailing spaces needs to be replaced by what ?
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: Fri Sep 05, 2008 9:11 pm
Reply with quote

Review this recent post -

ibmmainframes.com/viewtopic.php?t=32610

Bill
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri Sep 05, 2008 10:07 pm
Reply with quote

I guess the referred post serves another purpose.

You have a variable X(5) with value 'BILL '. You strip BILL and will have to put that into another variable X(5). Now the value is again 'BILL ' i.e. BILL followed by one space. You cannot just have only 'BILL' as there is no concept of varying char in COBOL so as to fit the data correctly depending on the lenght ...

If the OP wants the trailing spaces to be removed for some kind of checking or whtever the spaces needs to be removed by something else ...

As mentioned you can use PERFORM of REVERSE function to find the trailing spaces and then use MOVE or reference modification to convert the spaces to something else ...
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Sep 06, 2008 5:08 am
Reply with quote

Hello,

Quote:
I have a requirement in which i have to remove all the trailing spaces from an variable.
Where did this requirement originate?

How will the "field" (after the spaces are removed) be used? Is this data to be downloaded?

Might the requirement also include creating a delimited file for download? If the goal is a delimited file, the spaces can be removed and the next delimiter can be placed immediately after the last meaningful byte in the trailing-spaces-removed field(s).
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 Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts Remove leading zeroes SYNCSORT 4
No new posts leading spaces can be removed in trai... DFSORT/ICETOOL 1
No new posts Cobol program with sequence number ra... COBOL Programming 5
No new posts How to remove block of duplicates DFSORT/ICETOOL 8
Search our Forums:

Back to Top