IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Length of a variable in Cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
amitc23

New User


Joined: 05 Nov 2014
Posts: 95
Location: India

PostPosted: Tue Jan 12, 2016 2:12 pm
Reply with quote

Hi

Just need to clarify this.

I vaguely remember that any variable name followed by -length denotes the length of data in that variable. Is that correct ? I could not find this anywhere after search though.

Infact , I am dealing with a cobol program in which length of the variable is defined separately as NFT-MISC-DATA-LENGTH. while writing output to the file, only NFT-MISC-DATA is being populated. I am getting garbage in the variable length in the output, which is as expected.

But I see a number of Old records added in that file which have correct length. I checked a much older version of program and the code is same.

If -LENGTH followed by variable name does not automatically imply length of variable then I guess the length might have been written out from some other source.

Thanks
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Tue Jan 12, 2016 3:19 pm
Reply with quote

Code:
01  some-nice-data-name.
    05  orange COMP-5 PIC 9(4).
    05  the-actual-data PIC X(20000).


If that is a parameter, explicit or implicit, to something which expects the data to be like that (say SQL and an host-variable VARCHAR of maximum length 20,000 bytes) then that is how it would work. Name is irrelevant.
Code:

01  some-nice-data-name.
    05  apple BINARY PIC 9(8).
    05  the-actual-data.
        10   FILLER OCCURS 20000
            DEPENDING ON apple.
            15   FILLER PIC X.


That's your own variable-length field. The name, again, does not matter. Here, in addition, the location of apple does not matter (as long as it doesn't itself have a variable location).
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue Jan 12, 2016 6:11 pm
Reply with quote

Quote:
I vaguely remember that any variable name followed by -length denotes the length of data in that variable. Is that correct ?
It might be true for some version of COBOL but it is not true for Enterprise COBOL. Use the intrinsic function LENGTH in Enterprise COBOL.[/quote]
Back to top
View user's profile Send private message
jerryte

Active User


Joined: 29 Oct 2010
Posts: 202
Location: Toronto, ON, Canada

PostPosted: Tue Jan 19, 2016 12:37 am
Reply with quote

COBOL compiler does not automatically set the value of -LENGTH. You have to manually do it using
Code:
MOVE LENGTH OF NFT-MISC-DATA TO NFT-MISC-DATA-LENGTH

Note: the LENGTH OF construct is the byte length not the number of digits.
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Tue Jan 19, 2016 8:22 pm
Reply with quote

Remember that the LENGTH OF special register is the length of the PICTURE, not the length of the data inside the field. To get the length of the data, I usually use a combination of FUNCTION REVERSE and INSPECT xxx TALLYING LEADING SPACES.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Tue Jan 19, 2016 8:49 pm
Reply with quote

amitc23 has wandered off, so we'll probably never find out what they actually wanted.

LENGTH OF is equivalent to FUNCTION LENGTH except in the case of DBCS data, where FUNCTION LENGTH will give the length in characters and LENGTH OF the length in bytes.

Both operate on the length of the field, not the length of the data.

Both work with variable-length fields. LENGTH OF is an IBM Extension, FUNCTION LENGTH is from the COBOL Standard.

Except in the case of variable-length fields, both are resolved at compile time, because the compiler knows the length of fixed-length fields.

It is eminently possible that none of this relates to amitc23's problem.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
Search our Forums:

Back to Top