How to calculate STRING length?

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Shuku
Registered Member
Posts: 23
Joined: Sun Jul 21, 2013 4:55 am

How to calculate STRING length?

Post by Shuku »

Hi,

In Cobol, how can we find the length of the a string?

Say:

Code: Select all

01 A PIC X(10) 
And I've used only 7 bytes so the length be 7 but Cobol will tell only 10. How can we calculate that?

Also, I feel that in cobol it should not be called string lenght but then this is what i heard other saying it, is it correct? Please help me learn.
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: How to calculate STRING length?

Post by William Collins »

If we assume a "string" to be a binary-zero-delimited piecs of data (the binary zero usually represented as /n) then COBOL does not have that. COBOL has fixed-lenth fields, which will be "padded" to the right with spaces if the data is short of the field-length and truncated to the right if the data is longer than the field.

Enterprise COBOL does have string literals - consult the manual.

To find the length of the data in a field, you have to count the trailing spaces.

There are two main ways to do this: use INSPECT with TALLYING for LEADING SPACES on a FUNCTION REVERS field (INSPECT on the Mainfframe does not have TRAILING, altough some other compilers have it as a non-Standard addition)); code it out yourself.

If coding it out yourself, there are two main ways: user reference modification; use REDEFINES and a subscript/index.

I prefer the subscripting/indexing: check the field for SPACES first (makes termination condition easy); loop backwards until non-space (note that look will not process if first character in the loop (last byte of field) is non-space).
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: How to calculate STRING length?

Post by Robert Sample »

Also, I feel that in cobol it should not be called string lenght but then this is what i heard other saying it, is it correct? Please help me learn.
This terminology is not correct. A string in most languages can be variable length, while COBOL variables are generally fixed length. The only exceptions are for LINKAGE SECTION and FILE SECTION variables defined using OCCURS DEPENDING ON where the length of the data will depend on a variable. WORKING-STORAGE SECTION variables defined as OCCURS DEPENDING ON are generated with the maximum length possible, so they too are fixed length.
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: How to calculate STRING length?

Post by William Collins »

You can have a variable-length field (the amount of storage allocated is fixed) using OCCURS DEPENDING ON. It is still not a "string", because a string would be terminated, instead of this the variable-length field has a length.
Shuku
Registered Member
Posts: 23
Joined: Sun Jul 21, 2013 4:55 am

Re: How to calculate STRING length?

Post by Shuku »

Thank you so much for the explnation, they were very helping. I could not return to forums but I used something like this:

Code: Select all

01  WS-TEXT           PIC X(40) VALUE 'THIS IS THE VARIABLE VALUE'. 


INSPECT FUNCTION REVERSE(WS-TEXT) TALLYING L FOR LEADING SPACES 
COMPUTE LEN = LENGTH OF WS-TEXT - LEN 
Shuku
Registered Member
Posts: 23
Joined: Sun Jul 21, 2013 4:55 am

Re: How to calculate STRING length?

Post by Shuku »

William Collins wrote:You can have a variable-length field (the amount of storage allocated is fixed) using OCCURS DEPENDING ON. It is still not a "string", because a string would be terminated, instead of this the variable-length field has a length.
Sorry hit the submit button too fast. William sorry I did not understand your explnation, can you please share some example with me?
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: How to calculate STRING length?

Post by William Collins »

Try the following examples. Any questions, just ask.

Code: Select all

01  a-variable-length-field.
    05  filler occurs 0 to 30 times
        depending on length-of-the-field.
        10  filler PIC  x.

01  count-to-30 PIC X(30) VALUE
    "012345678901234567890123456789".

01  length-of-the-field binary pic 9(4).

MOVE 10 TO length-of-the-field
MOVE count-to-30 TO a-variable-length-field

DISPLAY 
        ">"
        a-variable-length-field
        "<"

MOVE 17 TO length-of-the-field
MOVE count-to-30 TO a-variable-length-field

DISPLAY 
        ">"
        a-variable-length-field
        "<"

MOVE ZERO TO length-of-the-field
MOVE count-to-30 TO a-variable-length-field

DISPLAY 
        ">"
        a-variable-length-field
        "<"
Shuku
Registered Member
Posts: 23
Joined: Sun Jul 21, 2013 4:55 am

Re: How to calculate STRING length?

Post by Shuku »

Thanks William. I shall not be able to try your example right away as I don't have a mainframe to test it but I understand the logic somewhat moving 10, 17 and zero to lengeth of the field is not clear to me. What is that doing?
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: How to calculate STRING length?

Post by zprogrammer »

Shuku Those are examples which William provided he had tried to showcase the vairable length fields value when it was depending on different lengths..

I would also suggest you to read the manual
zprogrammer
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: How to calculate STRING length?

Post by William Collins »

If you'd like to have your own COBOL compiler to try things out when you don't have access to a Mainframe, see here, https://sourceforge.net/p/open-cobol/di ... rce=navbar to download one. It is not Enterprise COBOL but it will allow you to try things out.

The examples are as Pandora-Box describes. As output from the DISPLAY you would see, bounded by > and <, date of 10, 17 and zero bytes.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”