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.
Length of data in the field
-
- Posts: 43
- Joined: Tue Dec 07, 2010 5:32 pm
- Skillset: COBOL,JCL,DB2,VSAM,CA7
- Referer: Google
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: Length of data in the field
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.
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.
- prino
- Posts: 641
- Joined: Wed Mar 11, 2009 12:22 am
- Skillset: PL/I - CICS - DB2 - IDMS - REXX - JCL, most in excess of three decades
- Referer: Google
- Location: Vilnius, Lithuania
- Contact:
Re: Length of data in the field
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
robert.ah.prins @ the.17+Gb.Google thingy
-
- Global moderator
- Posts: 3025
- Joined: Sun Jul 04, 2010 12:13 am
- Skillset: JCL, PL/1, Rexx, Utilities and to a lesser extent (i.e. I have programmed using them) COBOL,DB2,IMS
- Referer: Google
- Location: Pushing up the daisies (almost)
Re: Length of data in the field
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
Regards
Nic
-
- Posts: 43
- Joined: Tue Dec 07, 2010 5:32 pm
- Skillset: COBOL,JCL,DB2,VSAM,CA7
- Referer: Google
Re: Length of data in the field
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.
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)
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.
Code: Select all
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)

-
- Posts: 43
- Joined: Tue Dec 07, 2010 5:32 pm
- Skillset: COBOL,JCL,DB2,VSAM,CA7
- Referer: Google
Re: Length of data in the field
I think this ans if far more nearer to what interviewer wanted 

-
- Global moderator
- Posts: 3805
- Joined: Tue Jan 25, 2011 12:02 am
- Skillset: Easytrieve Plus, Cobol, Utilities, that sort of stuff
- Referer: Google
Re: Length of data in the field
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.
-
- Posts: 43
- Joined: Tue Dec 07, 2010 5:32 pm
- Skillset: COBOL,JCL,DB2,VSAM,CA7
- Referer: Google
Re: Length of data in the field
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
just for my curiosity will this code work for " HELLO " data in the 'source-string?(having spaces on both starting and ending
-
- Global moderator
- Posts: 3805
- Joined: Tue Jan 25, 2011 12:02 am
- Skillset: Easytrieve Plus, Cobol, Utilities, that sort of stuff
- Referer: Google
Re: Length of data in the field
Yes. The leading space, after the REVERSE, will be trailing and will not be counted by the INSPECT. The answer will be 6.
-
- Similar Topics
- Replies
- Views
- Last post
-
-
Convert fixed-length input records to variable-length output
by xcspg3 » Wed Oct 23, 2024 1:45 pm » in DFSORT/ICETOOL/ICEGENER - 5
- 1507
-
by sergeyken
View the latest post
Wed Oct 30, 2024 1:09 pm
-
-
-
Merge multiple fixed length files of different record length
by corvette1982 » Mon Mar 01, 2021 11:46 pm » in JCL - 8
- 5348
-
by willy jensen
View the latest post
Tue Mar 02, 2021 3:02 am
-
-
- 1
- 1815
-
by sergeyken
View the latest post
Fri Mar 26, 2021 11:59 pm
-
- 14
- 8537
-
by indianrajput
View the latest post
Wed Nov 24, 2021 11:33 pm
-
- 2
- 1609
-
by sergeyken
View the latest post
Sun Jan 26, 2025 3:07 am