Page 1 of 1

remove the trailing blanks and get the string length

PostPosted: Fri Mar 20, 2009 8:10 am
by qykong1986
Hello:
I have two issues.
fisrt see the following example.
01 ws-string pic x(50) value '012345679012345679     '---------there are five blanks after 789.

In procedure division I use
Inspect ws-string tallying ws-cnt for characters.
I found ws-cnt return 50, exceed my expectation 25.....

so ,my problem is :
1.how can i get the actual length of ws-string..in this example is 25.
2.Can anyone help to remove the trailing blanks in ws-string.


thanks ^_^.

Re: remove the trailing blanks and get the string length

PostPosted: Fri Mar 20, 2009 8:24 am
by dick scherrer
Hello and welcome to the forum,

1.how can i get the actual length of ws-string..in this example is 25.

The actual length is 50. . . To find the number of trailing spaces you could use:
INSPECT FUNCTION REVERSE(WS-STRING) TALLYING WS-CNT UNTIL INITIAL SPACE.
I've not tested this as i'm not connected to a mainframe just now.

2.Can anyone help to remove the trailing blanks in ws-string.
No. You might ignore them, but the data positions will still exist. What would you put in those positons if the blanks rwere "removed"?

If you more clearly explain what you really need to do, someone will probably have a suggestion. . .

Re: remove the trailing blanks and get the string length

PostPosted: Fri Mar 20, 2009 9:34 am
by qykong1986
Thank you for immediately reply.
INSPECT FUNCTION REVERSE(WS-STRING) TALLYING WS-CNT UNTIL INITIAL SPACE.

Sorry, saytax error happens, and the max condition code is 12.
I change it like this INSPECT FUNCTION REVERSE(WS-STRING) TALLYING WS-CNT FOR LEADING SPACE.
Though no saytax error, while WS-CNT return 30, not 5.....

My question is how can i get the last 5 charcters before trailing space .
for example
ws-string pic x(20) value '01234b01234bbbb' here b means blank, after some process ,it should return '01234'.
ws-string pic x(30) value''01234567901234bbbbbb' ,after some process, it should return '01234'.

so ,anyone could you help me to solve this problem, thanks ^-^.

Re: remove the trailing blanks and get the string length

PostPosted: Fri Mar 20, 2009 9:59 am
by dick scherrer
Hello,

Back to your original post. . . There is no 789 and there are 30 blanks at the end - not 5. You included 5 in the quotes, but the field is 50 bytes long and the unspecified charasters will have blanks.

If you want to get the 5 characters before the 30 blanks, you could use reference modification. Define a ws (i.e. ws-my5) numeric field and calculate the value as length - ws-cnt - 5. Then:
MOVE WS-STRING(WS-MY5:5) TO THE-TARGET-FIELD.

If you continue changing the examples, it will be a difficult target to hit ;)

Re: remove the trailing blanks and get the string length

PostPosted: Fri Mar 20, 2009 10:32 am
by qykong1986
Hi, thanks for your reply.
Define a ws (i.e. ws-my5) numeric field and calculate the value as length - ws-cnt - 5.

I think ws-my5 should be length - ws-cnt - 5 + 1.
:) If i was wrong, please correct me.

I asked my mentor, he taught me that i can use SAS to do data formatting.
For example:
WS-STRING PIC X(20) VALUE '01234BB789'
First use WS-STRING1 = TRIM(WS-STRING)----this will remove the trailing blanks, and only '01234BB789' will be moved to WS-STRING1.
Second use WS-RESULT = SUBSTR(WS-STRING1, start-position, length), after this i can get what i want.

Thanks ^_^.

Re: remove the trailing blanks and get the string length

PostPosted: Fri Mar 20, 2009 10:43 am
by qykong1986
dick scherrer wrote:Hello,

Back to your original post. . . There is no 789 and there are 30 blanks at the end - not 5. You included 5 in the quotes, but the field is 50 bytes long and the unspecified charasters will have blanks.

If you want to get the 5 characters before the 30 blanks, you could use reference modification. Define a ws (i.e. ws-my5) numeric field and calculate the value as length - ws-cnt - 5. Then:
MOVE WS-STRING(WS-MY5:5) TO THE-TARGET-FIELD.

If you continue changing the examples, it will be a difficult target to hit ;)


hi, you have already sovled my problem, just like what you say above.
Thanks ^_^.