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

Find the LENGTH of a String in COBOL


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

New User


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

PostPosted: Mon Jul 25, 2005 10:01 pm
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.
Back to top
View user's profile Send private message
thanooz

New User


Joined: 28 Jun 2005
Posts: 99

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

hi

there is interensic function

Code:
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
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: 1592

PostPosted: Tue Jul 26, 2005 6:18 am
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: 53
Location: India, Chennai

PostPosted: Tue Jul 26, 2005 12:31 pm
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
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: 53
Location: India, Chennai

PostPosted: Wed Jul 27, 2005 9:12 am
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: 1592

PostPosted: Sat Jul 30, 2005 7:44 pm
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: 25
Location: India

PostPosted: Sat Mar 29, 2008 10:34 pm
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

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Mar 29, 2008 11:00 pm
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

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Mar 29, 2008 11:09 pm
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: 1592

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

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

New User


Joined: 03 Feb 2006
Posts: 53
Location: Mumbai

PostPosted: Mon Mar 31, 2008 1:34 pm
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
meen

New User


Joined: 23 Jun 2008
Posts: 15
Location: bangalore

PostPosted: Fri Aug 14, 2009 11:02 pm
Reply with quote

Hi I tried implementing this:

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

But it is giving Maxxcc12 "TRAILING" was found in the "INSPECT" statement. The statement was discarded.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Aug 14, 2009 11:16 pm
Reply with quote

Hello,

Yup, you are not allowed to make up your own syntax. . .

At the top of the page is a link to "IBM Manuals" among which are the COBOL Language Reference and Programming Guide.

You might find REVERSE of interest. . .
Back to top
View user's profile Send private message
meen

New User


Joined: 23 Jun 2008
Posts: 15
Location: bangalore

PostPosted: Fri Aug 14, 2009 11:21 pm
Reply with quote

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

Again getting MAXCC12
"FUNCTION" was specified as an informational word in the current reserved word table. The reserved word table used may be different from the IBM-supplied default. Refer to VS COBOL II Application Programming Language
"FUNCTION" was not defined as a data-name.
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Fri Aug 14, 2009 11:33 pm
Reply with quote

Hello,

Quote:
Refer to VS COBOL II
The compiler being used is too old. . .

Another way to get the answer is to use a loop and count the spaces 1 by 1 from the end of the field until the first non-blank.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Aug 15, 2009 12:08 am
Reply with quote

Use reference-modification in an IN-LINE PERFORM, starting at the last byte of the STRING and proceed backwards, until the SUB is less than 1.

Define two WS fields, such as WS-SUB and WS-POS, as PIC 9(08) COMP fields, setting WS-SUB to THE LENGTH OF THE STRING and setting WS-POS to zero.

As you proceed backwards, when you find the position with the SPACE, move WS-SUB to WS-POS and MOVE zero to WS-SUB, which will terminate the PERFORM early.

After you come out of the PERFORM, WS-POS will equal zero (no SPACES found) or it will be non-zero, which will contain the position of first SPACE found beginning at the end of the STRING, proceeding backwards.

This is actually very rudimentary....

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

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sat Aug 15, 2009 12:33 am
Reply with quote

Use reference-modification in an IN-LINE PERFORM, starting at the last byte of the STRING and proceed backwards, until the SUB is less than 1.

Define two WS fields, such as WS-SUB and WS-POS, as PIC 9(08) COMP fields, setting WS-SUB to THE LENGTH OF THE STRING and setting WS-POS to ZERO.

As you proceed backwards, IF THE CHARACTER IS NOT = SPACE MOVE WS-SUB TO WS-POS AND MOVE zero to WS-SUB, which will terminate the PERFORM early.

After you come out of the PERFORM, WS-POS WILL BE CONTAIN THE LENGTH OF STRING EXCLUDING ANY TRAILING SPACES.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Aug 15, 2009 12:41 am
Reply with quote

Craig,

Oops! Logic was bass-ackwards! icon_redface.gif

Thanks for the correction.
Back to top
View user's profile Send private message
kranthikumarb

Active User


Joined: 02 Jan 2009
Posts: 115
Location: Hyderabad

PostPosted: Thu Nov 26, 2009 4:13 pm
Reply with quote

I know that this thread is quite old. But the way I looked for a solution after 3 years of the post, some one might want to know a solution after n years.

This is with out using any kind of functions.

Code:

 WORKING-STORAGE SECTION.                                       
   01 VAR1   PIC X(1000) VALUE SPACES.                           
   01 VAR2   PIC X(1000) VALUE SPACES.                           
   01 WS-LEN PIC 9(04) VALUE ZEROS.                             
   01 TOTAL PIC 9(04) VALUE ZEROS.                               
   01 I PIC 9(04).                                               
   01 J PIC 9(04).                                               
  PROCEDURE DIVISION.                                           
  100-MAIN-PARA.                                                 
        DISPLAY 'ENTER STRING :'                                 
        ACCEPT VAR1                                             
        MOVE 1 TO J                                             
* TO CALCULATE THE TOTAL LENGTH CAPACITY                                 
        INSPECT VAR1 TALLYING TOTAL FOR                         
        CHARACTERS BEFORE INITIAL '\0'                           
        DISPLAY 'TOTAL LENGTH CAPACITY OF THE VARIABLE IS ' TOTAL
        MOVE TOTAL TO I                                         
* STRING REVERSING                                               
         PERFORM UNTIL I = 0                                     
            MOVE VAR1(I:1) TO VAR2(J:1)                         
            COMPUTE J = J + 1                                   
            COMPUTE I = I - 1                                   
         END-PERFORM                                             
* END OF STRING REVERSING                                       
         INSPECT VAR2 TALLYING WS-LEN FOR LEADING SPACES         
         COMPUTE WS-LEN = TOTAL - WS-LEN                         
         DISPLAY 'LENGTH IS  :' WS-LEN                           
        STOP RUN.                                               


I am happy if this helps someone icon_smile.gif
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Thu Nov 26, 2009 4:31 pm
Reply with quote

Quote:
CHARACTERS BEFORE INITIAL '\0'
You are aware, I hope, that COBOL does not support the C standard of using a hex '00' to denote the end of a string? What you are looking for here is two characters -- a backwards slash followed by a zero (hex F0) -- not a single byte of hex 00. What C defines as \0 is called in COBOL a LOW-VALUES byte. Furthermore, 3270 emulators do not terminate string data input with anything so the data accepted by the program won't have a terminator, unless the person entering the data keys one in.
Back to top
View user's profile Send private message
kranthikumarb

Active User


Joined: 02 Jan 2009
Posts: 115
Location: Hyderabad

PostPosted: Thu Nov 26, 2009 9:07 pm
Reply with quote

Hi,
But, In the above code the value in the variable is 1000.
How can we explain this?

I just want to know how are we getting a value of 1000 in total if it \0 doesnt say that it is the end of the allocated size for the variable.

Regards,
Kranthi Kumar
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Thu Nov 26, 2009 10:40 pm
Reply with quote

Kranthi,

COBOL does not support the C standard of using a hex '00' to denote the end of a string

allocated size of variable is determined at compile time - not run time.

Admittedly, COBOL is not the best string manipulation language,
but if you would read the manuals, you won't have any problems.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Nov 27, 2009 5:17 am
Reply with quote

If you define the variable as PIC X(1000), the variable length is 1000. Not 1, not 10, not 100, not depending upon where a \0 occurs -- the length is 1000 bytes -- always and permanently. Unlike C and similar languages, you cannot change the length of the variable at run time -- it will always and forever be the length set at compile time.
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 Goto page 1, 2  Next

 


Similar Topics
Topic Forum Replies
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 COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
Search our Forums:

Back to Top