Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

To get the actual lentgth of the variable

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Sat Jun 04, 2011 3:56 pm    Post subject: To get the actual lentgth of the variable
Reply with quote

Hi,

I have a working storage variable ws-name of length 30.

It is not populated with any value.

Is there any way to get the actual length of the variable?

Because Length of gives only the length of the populated data. i.e if ws-name is populated with INDIA. the length of gives the length as 5 but actually i need 30.
Back to top
View user's profile Send private message

Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jun 04, 2011 4:08 pm    Post subject:
Reply with quote

What are you trying to do?

Can you support what you said?

Please code "DISPLAY Length>" LENGTH OF 30-BYTE-ITEM "< "
Contents>" 30-BYTE-ITEM "<"

With 30-BYTE-ITEM defined in Working-Storage in the obvious way.
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Sat Jun 04, 2011 4:23 pm    Post subject:
Reply with quote

Hi Bill,
I cannot hardcode the length in working storage.

The length of the variable can change. so it has to be dynamic.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jun 04, 2011 4:31 pm    Post subject:
Reply with quote

Hi Santosh,

Can you be a little more exact about what you want?

You have a 30 byte field, but you want to find how much "data" you have in it, excluding trailing blanks? Something like that?

In general Cobol is pretty good at not having variable length data items. Groups, OK, with Occurs Depending On.
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Sat Jun 04, 2011 4:41 pm    Post subject:
Reply with quote

No Bill,

I have a 30 byte field which is 30 byte now and can change to 40 byte next year.
So i have to get this length in a dynamic way.
Is there any command which gives the actual length of the variable?
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jun 04, 2011 4:52 pm    Post subject:
Reply with quote

No, still not getting it.

If you define a field as X(30) it can only change if someone does it. Can't do it on its own.

What do you mean by "the variable"? Do you mean the length of the field (changes only by changing source code) or of the data?

Did you try the DISPLAY I showed you?
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jun 04, 2011 5:09 pm    Post subject:
Reply with quote

Santosh,

The answer to your question will almost certainly be yes. But, for the details, we need to know what you want to do, clearly.

Try to show some examples, using the DISPLAY, of what you mean.
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Sat Jun 04, 2011 5:33 pm    Post subject:
Reply with quote

Ok Bill, here is an example.

i have a variable in working storage as

WS-LENGTH-NAME PIC 9(02)

I agree that the size changes only if somebody changes it frm 30 to 40.

But say WS-NAME has a corresponding table variable OR some other variable
WS-NAME PIC X(30). The length of this table variable changes from 30 to 40.
Then this change has to be identified in the program which reflects in the WS-LENGTH-NAME variable.

Hence i would like to get the actual length of the variable WS-NAME into WS-LENGTH-NAME

But Length of String gives the length of the data populated.
Ex. if ws-NAME is filled with string of 20 characters.

It returns 20 but i need 30.

Thank you for your interest in answering this question.
icon_smile.gif
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jun 04, 2011 5:42 pm    Post subject:
Reply with quote

Have you actually tried LENGTH OF or is that your understanding of what it says in the manual?

The "old" way was to define the field with a value, put it close to the field it is the length of (or number of occurences of) and hope that no-one changes one without the other.

The "new" way is what you want. Check out this week's postings in the Cobol forum, do a seach for "LENGTH OF" and you'll find one by Bill O'Boyle that is using LENGTH OF to find the maximum number of occurences of a table without having to "hard code" it in the program. Same should work for your case.

By the way, don't define it as unsigned display.

Try COMP-5/COMP/BINARY PIC S9(4), it will save lots of conversions and stuff generated by the compiler as you use the field.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Sat Jun 04, 2011 5:45 pm    Post subject:
Reply with quote

Quote:
But Length of String gives the length of the data populated.
Ex. if ws-NAME is filled with string of 20 characters.

It returns 20 but i need 30.


this is a cobol forum, so
length of variable-name /or length of 'literal'
always returns the compiled length of the variable

there is no such thing in cobol as LENGTH OF STRING

if you want to determine the length of a value within a variable,
one way,
determine the number of trailing spaces within the variable
Code:
INSPECT(REVERSE(variable)) TALLYING LEADING SPACES.

and subtract that from the LENGTH OF variable.

you are suffereing from a false understanding of the intrinsic function LENGTH OF.
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Sat Jun 04, 2011 6:04 pm    Post subject:
Reply with quote

Quote:
Code:
INSPECT(REVERSE(variable)) TALLYING LEADING SPACES.

Thank you for the replies.
Bill, yes. Now i understood it properly.
Thank you for the valuable information.

Even the above code given by senior member was helpful.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Sat Jun 04, 2011 6:28 pm    Post subject: Reply to: To get the actual lentgth of the variable
Reply with quote

No problem. I think "Also" rather than "Even" for the Senior Member.

Only thing is if you have "usage NATIONAL" you might need intrinsic function LENGTH. Gives a different answer for this USAGE. Just for completeness.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7913
Location: Bellevue, IA

PostPosted: Sat Jun 04, 2011 8:25 pm    Post subject:
Reply with quote

COBOL does not have "strings" like C, Perl, and other languages. Every variable's length is fixed at compile time and cannot be changed at run time, unless you use OCCURS DEPENDING ON and the variable is in LINKAGE SECTION. Hence the LENGTH function returns the compile length, not the populated length. For WORKING-STORAGE variables, even OCCURS DEPENDING ON items are fixed length -- COBOL reserves the maximum required amount of storage for the variable -- although the length returned probably depends upon the ODO variable (I'm not at a place where I can check that right now).
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10202
Location: italy

PostPosted: Sat Jun 04, 2011 9:33 pm    Post subject: Reply to: To get the actual lentgth of the variable
Reply with quote

Quote:
Because Length of gives only the length of the populated data. i.e if ws-name is populated with INDIA. the length of gives the length as 5 but actually i need 30.

it would be nice if You could post evidence quoting the exact manual paragraph icon_evil.gif

since You cannot do it, it will be more useful/wiser to review the manual for a better understanding of LENGTH OF

here is the link to the ( special register ) LENGTH OF manual page
http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/igy3lr31/1.3.8.4?ACTION=MATCHES&REQUEST=length&TYPE=FUZZY&SHELF=IGY3SH33.bks&DT=20060329140556&CASE=&searchTopic=TOPIC&searchText=TEXT&searchIndex=INDEX&rank=RANK&ScrollTOP=FIRSTHIT#FIRSTHIT

and ( for clarity ) to the FUNCTION LENGTH
http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/igy3lr31/7.1.27?ACTION=MATCHES&REQUEST=length&TYPE=FUZZY&SHELF=IGY3SH33.bks&DT=20060329140556&CASE=&searchTopic=TOPIC&searchText=TEXT&searchIndex=INDEX&rank=RANK&ScrollTOP=FIRSTHIT#FIRSTHIT
Back to top
View user's profile Send private message
ds Senthil

New User


Joined: 19 May 2011
Posts: 3
Location: India

PostPosted: Mon Jun 13, 2011 9:53 am    Post subject: Reply to: To get the actual lentgth of the variable
Reply with quote

Hi Santhosh,
'LENGTH OF' will actually return the length defined for the variable in Working storage.

for example

05 WS-LNGTH-TEST PIC X(30).

in PD if you display the

DISPLAY 'length of variable = ' LENGTH OF WS-LNGTH-TEST.

The displayed value will be 30 irrespective of whether it is populated or not.

The difficult and dynamic part is finding the populated length if the variable. For this you may have to find the first populated byte from reverse and the position of that byte would be the length if the populated area.

hope this clarifies.

Cheers
Back to top
View user's profile Send private message
santosh ks

New User


Joined: 27 May 2011
Posts: 16
Location: india

PostPosted: Mon Jun 13, 2011 10:44 am    Post subject:
Reply with quote

Hi senthil,
Thank you. This clarified my issue...
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7232

PostPosted: Mon Jun 13, 2011 12:15 pm    Post subject: Re: Reply to: To get the actual lentgth of the variable
Reply with quote

ds Senthil wrote:

[...]

'LENGTH OF' will actually return the length defined for the variable in Working storage.

[...]


Are you saying it doesn't work elsewhere in the Data Division?
Back to top
View user's profile Send private message
Anuj Dhawan

Senior Member


Joined: 22 Apr 2006
Posts: 6258
Location: Mumbai, India

PostPosted: Mon Jun 13, 2011 5:29 pm    Post subject:
Reply with quote

Leave him, Bill icon_wink.gif.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1219
Location: Israel

PostPosted: Mon Jun 13, 2011 8:37 pm    Post subject:
Reply with quote

Just to make sure there are no lose ends:

Assuming the variable is defined in a copybook, and this copybook is used in your program,
If somebody changes the length of the variable in the copybook, your program will not be aware of it until it is recompiled.

If the copybook originally contains: 05 WS-LENGTH-TEST PIC X(30).
then your program contains: 05 WS-LENGTH-TEST PIC X(30).
and LENGTH OF WS-LENGTH-TEST is equal to 30.

If the copybook is modified and now contains: 05 WS-LENGTH-TEST PIC X(50). ,
your program still has: 05 WS-LENGTH-TEST PIC X(30).
and LENGTH OF WS-LENGTH-TEST is still equal to 30.
(This, in many cases, creates a little bug...)

If you just compile the program without any other change,
it (the program) will contain: 05 WS-LENGTH-TEST PIC X(50).
and only then LENGTH OF WS-LENGTH-TEST will be equal to 50.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Format to pipe delimited with variabl... pshongal SYNCSORT 6 Wed Sep 14, 2016 2:48 pm
No new posts Variable for cursor names ravikumar15 DB2 5 Sun Jul 17, 2016 7:08 am
No new posts OPC JCL Variable passion_sujesh IBM Tools 5 Fri Jul 01, 2016 4:21 am
No new posts JOINKEYs and Variable files migusd SYNCSORT 8 Fri Jun 17, 2016 3:16 am
No new posts File Manager with Variable Length File ahabib IBM Tools 10 Thu Mar 31, 2016 11:00 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us