View previous topic :: View next topic
|
Author |
Message |
passion_sujesh
New User
Joined: 14 Mar 2008 Posts: 74 Location: India,Chennai
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
ssk1711
New User
Joined: 16 Jun 2008 Posts: 40 Location: bangalore
|
|
|
|
Hi
I think you can use VARCHAR.
Please specify exact requirement, with an example. |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
ssk1711 wrote: |
Hi
I think you can use VARCHAR.
Please specify exact requirement, with an example. |
VARCHAR in COBOL? |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
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 |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
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 |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
Back to top |
|
|
ashimer
Active Member
Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
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 |
|
|
|