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.

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.

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).

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.

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.