View previous topic :: View next topic
|
Author |
Message |
hariibm
New User
Joined: 05 Feb 2007 Posts: 61 Location: Chennai
|
|
|
|
Hi All,
Please provide me with a peace of code to get the length of a character string.(without using INSPECT verb).
Ex:
01 A PIC X(40).
MOVE 'IBM FORUMS.COM' TO A.
I know LENGTH OF gives the length as 40. But it should give only 14 as the actual length of the string is 14.
Thanks,
Hari |
|
Back to top |
|
|
William Thompson
Global Moderator
Joined: 18 Nov 2006 Posts: 3156 Location: Tucson AZ
|
|
|
|
Something like this could be used where x is a counter and length will be your length:
perform varying x from 40 by -1 until x is less than 1
if a(x:1) not = space move x to length move zero to x end-if
end-perform |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
Hi hariibm,
Give this a try:
Code: |
01 TEXT1 PIC X(010) VALUE '12AB'.
INSPECT FUNCTION REVERSE(TEXT1) TALLYING L FOR LEADING SPACES
COMPUTE L = LENGTH OF TEXT1 - L |
After the INSPECT L contains a 6.
After the COMPUTE it contains 4 (10 - 6), the length of the string (12AB). |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Jack, hari wants it without using INSPECT verb.
Following code should serve the purpose
Code: |
PERFORM VARYING I FROM 39 BY -1
UNTIL A(I:1) NOT= SPACES
END-PERFORM
DISPLAY 'LENGTH OF STR : ' I.
|
I have hardcoded 39 in the loop. We can use one var that contains value (LENGTH OF A - 1) |
|
Back to top |
|
|
agkshirsagar
Active Member
Joined: 27 Feb 2007 Posts: 691 Location: Earth
|
|
|
|
Sorry
A little correction in above code.
Code: |
PERFORM VARYING I FROM LENGTH OF A BY -1
UNTIL A(I:1) NOT= SPACES
END-PERFORM
DISPLAY 'LENGTH OF STR : ' I.
|
|
|
Back to top |
|
|
|