View previous topic :: View next topic
|
Author |
Message |
santosh ks
New User
Joined: 27 May 2011 Posts: 16 Location: india
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
santosh ks
New User
Joined: 27 May 2011 Posts: 16 Location: india
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
santosh ks
New User
Joined: 27 May 2011 Posts: 16 Location: india
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
santosh ks
New User
Joined: 27 May 2011 Posts: 16 Location: india
|
|
|
|
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.
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
santosh ks
New User
Joined: 27 May 2011 Posts: 16 Location: india
|
|
|
|
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 |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
Back to top |
|
|
ds Senthil
New User
Joined: 19 May 2011 Posts: 3 Location: India
|
|
|
|
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 |
|
|
santosh ks
New User
Joined: 27 May 2011 Posts: 16 Location: india
|
|
|
|
Hi senthil,
Thank you. This clarified my issue... |
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
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 |
|
|
Anuj Dhawan
Superior Member
Joined: 22 Apr 2006 Posts: 6250 Location: Mumbai, India
|
|
|
|
Leave him, Bill . |
|
Back to top |
|
|
Marso
REXX Moderator
Joined: 13 Mar 2006 Posts: 1353 Location: Israel
|
|
|
|
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 |
|
|
|