Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
How to find the string length in COBOL

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Mainframe COBOL
Author Message
jagadeshdvn

New User


Joined: 27 Jun 2005
Posts: 12
Location: East Hartford

PostPosted: Mon Jul 25, 2005 10:01 pm    Post subject: How to find the string length in COBOL
Reply with quote

Hi,

Please let me know, How to find the length of a string in COBOL? Appreciate if you can explain the syntax with an example.

Thanks in advance!!
Back to top
View user's profile Send private message
References
thanooz

Active User


Joined: 28 Jun 2005
Posts: 105

PostPosted: Mon Jul 25, 2005 10:07 pm    Post subject:
Reply with quote

hi

there is interensic function

function length(variablename).





thanooz
Back to top
View user's profile Send private message
jagadeshdvn

New User


Joined: 27 Jun 2005
Posts: 12
Location: East Hartford

PostPosted: Mon Jul 25, 2005 10:17 pm    Post subject: Got It: (How to find the string length in COBOL)
Reply with quote

Thanks Thanooz. It nice of u buddy!!

Just to add for reference

Finding the length of data items
You can use the LENGTH function in many contexts (including numeric data and tables) to determine the length of string items.
The following COBOL statement demonstrates moving a data item into that field in a record that holds customer names:
Move Customer-name To Customer-record(1:Function Length(Customer-name))
You could also use the LENGTH OF special register. Coding either Function
Length(Customer-Name) or LENGTH OF Customer-Name returns the same result: the length of Customer-Name in bytes.
You can use the LENGTH function only where arithmetic expressions are allowed. However, you can use the LENGTH OF special register in a greater variety of contexts. For example, you can use the LENGTH OF special register as an argument to an intrinsic function that allows integer arguments. (You cannot use an intrinsic function as an operand to the LENGTH OF special register.) You can also use the LENGTH OF special register as a parameter in a CALL statement.
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1504

PostPosted: Tue Jul 26, 2005 6:18 am    Post subject:
Reply with quote

Here's another take on the question.
Code:

01  TEXT1           PIC X(40) VALUE 'THIS IS THE STRING'.


INSPECT FUNCTION REVERSE(TEXT1) TALLYING L FOR LEADING SPACES
COMPUTE L = LENGTH OF TEXT1 - L

L will contain the length of the string in TEST1.
Back to top
View user's profile Send private message
Rameshs

New User


Joined: 15 Jun 2005
Posts: 49
Location: India, Chennai

PostPosted: Tue Jul 26, 2005 12:31 pm    Post subject: Getting error
Reply with quote

"Length of dataname" is working But if i use Function Reverse(dataname) i am getting the error as follows

GYPS2121-S "FUNCTION" was not defined as a data-name.
Back to top
View user's profile Send private message
pruthvi

New User


Joined: 05 Mar 2005
Posts: 4

PostPosted: Tue Jul 26, 2005 7:08 pm    Post subject: Re: How to find the string length in COBOL
Reply with quote

hi,

for me the above code is working fine and i got the correct output.
i tested the code in visual age ccobol
Back to top
View user's profile Send private message
Rameshs

New User


Joined: 15 Jun 2005
Posts: 49
Location: India, Chennai

PostPosted: Wed Jul 27, 2005 9:12 am    Post subject: Re: How to find the string length in COBOL
Reply with quote

Hai pruthvi, icon_cool.gif

I am coding on mainframe environment using IGYWCL cobol compiler
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1504

PostPosted: Sat Jul 30, 2005 7:44 pm    Post subject:
Reply with quote

Intrinsic functions were introduced to mainframe COBOL with COBOLII.
Back to top
View user's profile Send private message
Anurag Singh

New User


Joined: 20 Jan 2008
Posts: 21
Location: India

PostPosted: Sat Mar 29, 2008 10:34 pm    Post subject:
Reply with quote

Hi,
How to use the interensic function

function length(variablename).


where it will give the length of the variable??
Back to top
View user's profile Send private message
dick scherrer

Global Moderator


Joined: 23 Nov 2006
Posts: 8652
Location: 221 B Baker St

PostPosted: Sat Mar 29, 2008 11:00 pm    Post subject:
Reply with quote

Hello,

It will "give the length" into whatever field you tell it to. . . What you posted is only part of the code needed to retrieve the length.

Code:
compute lth-of-fld = function length(variablename).

or
Code:
compute lth-of-fld = length of variablename.


Keep in mind that this will give the length of the field, not the length of the data in the field. If the field is defined pic x(10) and the content is 'AAA', the length returned by the function is 10.

If you want the length of the content, you need more code. There are several examples in the COBOL section of the forum, so pick one that works for you.
Back to top
View user's profile Send private message
Bill O'Boyle

Senior Member


Joined: 14 Jan 2008
Posts: 334
Location: Orlando, FL, USA

PostPosted: Sat Mar 29, 2008 11:09 pm    Post subject: Re: How to find the string length in COBOL
Reply with quote

mmwife wrote:
Intrinsic functions were introduced to mainframe COBOL with COBOLII.

Jack,

A slight correction.

COBOL/370 (the successor to COBOL2) is the version/release where FUNCTIONS were first introduced.

Regards,

Bill
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1504

PostPosted: Mon Mar 31, 2008 2:20 am    Post subject:
Reply with quote

Thanx Bill. My bad.
Back to top
View user's profile Send private message
Aji

Active User


Joined: 03 Feb 2006
Posts: 53
Location: Mumbai

PostPosted: Mon Mar 31, 2008 1:34 pm    Post subject: Reply to: How to find the string length in COBOL
Reply with quote

Hi
This method is without using 'Function'.

Inspect Text1 Tallying L For Trailing Spaces.
Lenght = Length of Text1 - L.



Aji
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Mainframe COBOL All times are GMT + 6 Hours
Page 1 of 1