Length of data in the field



Length of data in the field

Postby raghuvanshi » Sun Oct 28, 2012 4:40 pm

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.
raghuvanshi
 
Posts: 43
Joined: Tue Dec 07, 2010 5:32 pm
Has thanked: 3 times
Been thanked: 0 time

Re: Length of data in the field

Postby Robert Sample » Sun Oct 28, 2012 6:26 pm

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.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Length of data in the field

Postby prino » Sun Oct 28, 2012 7:15 pm

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?
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy
User avatar
prino
 
Posts: 641
Joined: Wed Mar 11, 2009 12:22 am
Location: Vilnius, Lithuania
Has thanked: 3 times
Been thanked: 29 times

Re: Length of data in the field

Postby NicC » Sun Oct 28, 2012 7:34 pm

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.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Length of data in the field

Postby raghuvanshi » Sun Oct 28, 2012 8:03 pm

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) :(
raghuvanshi
 
Posts: 43
Joined: Tue Dec 07, 2010 5:32 pm
Has thanked: 3 times
Been thanked: 0 time

Re: Length of data in the field

Postby raghuvanshi » Sun Oct 28, 2012 8:19 pm

I think this ans if far more nearer to what interviewer wanted :)
raghuvanshi
 
Posts: 43
Joined: Tue Dec 07, 2010 5:32 pm
Has thanked: 3 times
Been thanked: 0 time

Re: Length of data in the field

Postby BillyBoyo » Sun Oct 28, 2012 9:00 pm

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.

These users thanked the author BillyBoyo for the post:
raghuvanshi (Sun Oct 28, 2012 9:51 pm)
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Length of data in the field

Postby raghuvanshi » Sun Oct 28, 2012 10:08 pm

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
raghuvanshi
 
Posts: 43
Joined: Tue Dec 07, 2010 5:32 pm
Has thanked: 3 times
Been thanked: 0 time

Re: Length of data in the field

Postby BillyBoyo » Sun Oct 28, 2012 11:44 pm

Yes. The leading space, after the REVERSE, will be trailing and will not be counted by the INSPECT. The answer will be 6.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times


Return to Interview Questions

 


  • Related topics
    Replies
    Views
    Last post