Page 1 of 1

Length of data in the field

PostPosted: Sun Oct 28, 2012 4:40 pm
by raghuvanshi
Hi ,

Another question I have doubt in is(which was asked in interview)
How to find length of variable in COBOL?
My answer was FUNCTION LENGTH(var1) gives the length of the field, which I think is wrong!
what I feel is that he wanted to ask length of data in the field.
What would be the answer if we want to find the length of data in the field and not only the size of field?
Please suggest.

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 6:26 pm
by Robert Sample
First, COBOL does not support variable length data variables in WORKING-STORAGE -- every variable's length is the size of the variable, period. This includes variables defined as OCCURS DEPENDING ON since COBOL allocates storage equal to the maximum size of the variable.

Second, FILE SECTION and LINKAGE SECTION variables may be variable length if they contain OCCURS DEPENDING ON. The Enterprise COBOL Language Reference manual (link at the top of this page) discusses in detail what LENGTH OF and FUNCTION LENGTH return for such a variable.

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 7:15 pm
by prino
raghuvanshi wrote:My answer was FUNCTION LENGTH(var1) gives the length of the field, which I think is wrong!
what I feel is that he wanted to ask length of data in the field.

And how do you define the length of the data. Why for example would trailing blanks not be significant?

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 7:34 pm
by NicC
If it is deemed that trailing blanks are not part of the data that you are interested you can reverse the data and then find the first non-blank character.

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 8:03 pm
by raghuvanshi
Hi,
While searching on internet I came across following code:

Source-string = " SSET"
If we need to get string without spaces, trim the string. we can use
following logic.
   INSPECT FUNCTION REVERSE (Source-string) TALLYING space-count FOR LEADING SPACES.
   COMPUTE length-of-string = 6 -  space-count.
   Move Source-string(space-count+1 : length-of-string )  TO ws-target-string.

Above INSPECT command get the no of leading spaces from the string. after executing the INSPECT command space-count variable contains 2.
In compute statement, space-count subtracted from length of Source-string. value 4 will be stored in length-of-string.
In move statement, Using reference modification, moved actual string to ws-target-string. removed spaces.

But for this code we need to know how many characters are there in the 'source-string' that we actually want(4 here)
I guess Robert you are right there is no way I found so far to get the desired result(actual length of the data) :(

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 8:19 pm
by raghuvanshi
I think this ans if far more nearer to what interviewer wanted :)

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 9:00 pm
by BillyBoyo
LENGTH of or FUNCTION LENGTH will tell you the length of the field itself (the maximum number of bytes/characters that can be held). With the REVERSE and INSPECT as shown, you can find the "trailing" spaces. If you want the length of the data excluding trailing spaces, replace the "6" with the value found from the LENGTH. You then don't have to change the value of the literal if the length of the field changes.

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 10:08 pm
by raghuvanshi
Thanks Billy that's a good idea!
just for my curiosity will this code work for " HELLO " data in the 'source-string?(having spaces on both starting and ending

Re: Length of data in the field

PostPosted: Sun Oct 28, 2012 11:44 pm
by BillyBoyo
Yes. The leading space, after the REVERSE, will be trailing and will not be counted by the INSPECT. The answer will be 6.