Page 1 of 1

How to calculate STRING length?

Posted: Mon Jun 09, 2014 11:56 am
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.

Re: How to calculate STRING length?

Posted: Mon Jun 09, 2014 12:18 pm
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).

Re: How to calculate STRING length?

Posted: Tue Jun 10, 2014 10:53 pm
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.

Re: How to calculate STRING length?

Posted: Wed Jun 11, 2014 1:37 pm
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.

Re: How to calculate STRING length?

Posted: Mon May 25, 2015 1:51 pm
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 

Re: How to calculate STRING length?

Posted: Mon May 25, 2015 2:10 pm
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?

Re: How to calculate STRING length?

Posted: Mon May 25, 2015 2:32 pm
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
        "<"

Re: How to calculate STRING length?

Posted: Tue May 26, 2015 1:52 pm
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?

Re: How to calculate STRING length?

Posted: Tue May 26, 2015 1:59 pm
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

Re: How to calculate STRING length?

Posted: Tue May 26, 2015 3:29 pm
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.