redifine & occur



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

redifine & occur

Postby Vikrant Survase » Tue Jun 08, 2010 8:55 pm

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?
Vikrant Survase
 
Posts: 19
Joined: Fri Jun 04, 2010 9:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: redifine & occur

 

Re: redifine & occur

Postby Robert Sample » Tue Jun 08, 2010 9:08 pm

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.
Robert Sample
Global moderator
 
Posts: 3378
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 225 times

Re: redifine & occur

Postby Vikrant Survase » Tue Jun 08, 2010 9:27 pm

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
Vikrant Survase
 
Posts: 19
Joined: Fri Jun 04, 2010 9:08 pm
Has thanked: 0 time
Been thanked: 0 time

Re: redifine & occur

Postby Robert Sample » Tue Jun 08, 2010 10:37 pm

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.
Robert Sample
Global moderator
 
Posts: 3378
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 225 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post