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

Find the Length of the character string without using STRING


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

New User


Joined: 05 Feb 2007
Posts: 61
Location: Chennai

PostPosted: Thu Mar 01, 2007 11:31 pm
Reply with quote

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
View user's profile Send private message
William Thompson

Global Moderator


Joined: 18 Nov 2006
Posts: 3156
Location: Tucson AZ

PostPosted: Thu Mar 01, 2007 11:48 pm
Reply with quote

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
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Fri Mar 02, 2007 8:13 am
Reply with quote

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
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Fri Mar 02, 2007 1:09 pm
Reply with quote

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
View user's profile Send private message
agkshirsagar

Active Member


Joined: 27 Feb 2007
Posts: 691
Location: Earth

PostPosted: Fri Mar 02, 2007 1:29 pm
Reply with quote

Sorry icon_redface.gif
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
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 Store the data for fixed length COBOL Programming 1
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
Search our Forums:

Back to Top