remove the trailing blanks and get the string length



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

remove the trailing blanks and get the string length

Postby qykong1986 » Fri Mar 20, 2009 8:10 am

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 ^_^.
--------------------------------------------------------------------------------------------------------------
Thanks ^_^
Hubery.
qykong1986
 
Posts: 10
Joined: Thu Mar 19, 2009 11:28 am
Location: Shanghai.
Has thanked: 0 time
Been thanked: 0 time

Re: remove the trailing blanks and get the string length

 

Re: remove the trailing blanks and get the string length

Postby dick scherrer » Fri Mar 20, 2009 8:24 am

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. . .
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: remove the trailing blanks and get the string length

Postby qykong1986 » Fri Mar 20, 2009 9:34 am

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 ^-^.
--------------------------------------------------------------------------------------------------------------
Thanks ^_^
Hubery.
qykong1986
 
Posts: 10
Joined: Thu Mar 19, 2009 11:28 am
Location: Shanghai.
Has thanked: 0 time
Been thanked: 0 time

Re: remove the trailing blanks and get the string length

Postby dick scherrer » Fri Mar 20, 2009 9:59 am

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 ;)
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: remove the trailing blanks and get the string length

Postby qykong1986 » Fri Mar 20, 2009 10:32 am

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 ^_^.
--------------------------------------------------------------------------------------------------------------
Thanks ^_^
Hubery.
qykong1986
 
Posts: 10
Joined: Thu Mar 19, 2009 11:28 am
Location: Shanghai.
Has thanked: 0 time
Been thanked: 0 time

Re: remove the trailing blanks and get the string length

Postby qykong1986 » Fri Mar 20, 2009 10:43 am

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 ^_^.
qykong1986
 
Posts: 10
Joined: Thu Mar 19, 2009 11:28 am
Location: Shanghai.
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post