Page 1 of 1

redifine & occur

PostPosted: Tue Jun 08, 2010 8:55 pm
by Vikrant Survase
hi friends,

how can u redefine pic x(10) with pic 9(6)?

and one more

Is this allowed?

01 WS-TABLE.

03 FILLER-X PIC X(5) VALUE 'AAAAA'.

03 WS-EX REDEFINES FILLER-X OCCURS 5 TIMES PIC X(1).

can redefines clause be used under occurs clause or vice versa?

Re: redifine & occur

PostPosted: Tue Jun 08, 2010 9:08 pm
by Robert Sample
REDEFINE is used to allow referencing the same memory locations by multiple variables. Hence this code is legal although it could have negative consequences when executed:
05  X  PIC X(10).
05  Y  REDEFINES X
       PIC 9(06).
and it is a very good idea to ensure the base and redefined variables occupy the same number of byte even though it is not required.

Occurs can be used with a redefintiion, although OCCURS DEPENDING ON cannot.

Re: redifine & occur

PostPosted: Tue Jun 08, 2010 9:27 pm
by Vikrant Survase
01 WS-VARX PIC X(10) VALUE 'ABCDEFGHIJ'.
01 WS-VARN REDEFINES WS-VARX PIC 9(5) VALUE '12345'.

What will happen I want Display WS-VARX and WS-VARN?

WILL IT DISPLAY
WS-VARX -12345FGHIJ
AND WS-VARN-12345

Re: redifine & occur

PostPosted: Tue Jun 08, 2010 10:37 pm
by Robert Sample
From the COBOL Language Reference manual, section 5.3.18.1 on the VALUE clause:
The VALUE clause must not be specified for a data description entry that contains or is subordinate to an entry that contains either an EXTERNAL or a REDEFINES clause. This rule does not apply to condition-name entries.
so you will get a syntax error if you attempt this.

The mere fact that you can ask such a question indicates you have no understanding at all of REDEFINES. In your example, WS-VARK and WS-VARN are not separate variables -- they are a single area of memory that has two names. If you move something to WS-VARK, you have changed the value of WS-VARN. If you move something to WS-VARN, you have changed the value of WS-VARK (or, at least, the first five bytes of WS-VARK). There is no distinction between the two. If you display them, you will find WS-VARN has the same value as the first five bytes of WS-VARK.

And before you ask, a PIC 9(05) variable that is USAGE DISPLAY will display alphabetic characters just fine. If you attempt to do arithmetic with the field, you will find the value is 12345 due to the internal representation of USAGE DISPLAY numeric variables and alphanumeric variables.