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
 

 

Redefines in cobol

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

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Fri Jan 08, 2010 8:31 pm    Post subject: Redefines in cobol
Reply with quote

Hi,


01 WS-VAR1 PIC X(5).
01 WS-VAR2 REDEFINES WS-VAR1 PIC 9(3).
.
.

MOVE "HELLO" TO WS-VAR1.
DISPLAY WS-VAR1.
DISPLAY WS-VAR2.
.
.

will give out
HELLO
HEL

Please help me out what the logic happens behind and how the ws-var2 is HEL.

Thanks,
L.Nethaji
Back to top
View user's profile Send private message

Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2502
Location: Atlanta, Georgia, USA

PostPosted: Fri Jan 08, 2010 8:37 pm    Post subject: Reply to: Redefines in cobol
Reply with quote

WS-VAR1 is FIVE bytes and WS-VAR2 is THREE bytes icon_exclaim.gif

The DISPLAY verb uses the length of the target field when performing the display.

Is this a trick question?
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Fri Jan 08, 2010 9:29 pm    Post subject:
Reply with quote

It is a very common misconception that COBOL variables defined as PIC 9 can only contain numeric values. This is not true. A USAGE DISPLAY variable, such as you have defined as WS-VAR2, may contain numbers, letters, spaces, or special symbols. If you attempt to use that variable for arithmetic while it contains non-numeric data, you will get a S0C7 (usually) indicating non-numeric data in a numeric field. But just moving another variable into the variable and displaying the variable does not cause any issues in COBOL if the PIC 9 field does not contain numbers. This is not recommended since it is a bad coding practice but it is not, under COBOL's rules, illegal.
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Fri Jan 08, 2010 10:39 pm    Post subject: Thanks for the reply
Reply with quote

Hi Robert,

I had a very clear understanding of redefines now .

Even i too had in mind like the variable declared as 9 should have numeric values.

Thanks a lot for ur reply,

l.nethaji
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Fri Jan 08, 2010 10:42 pm    Post subject:
Reply with quote

Glad to hear it helped!
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Fri Jan 08, 2010 10:44 pm    Post subject: Redefines in cobol
Reply with quote

Hi Robert,

I had a small clarification on the above

If ws-var1 has value 'ABC' and Ws-var2 had 123 .

What will be the value of both if we redefines as the same.

01 WS-VAR1 PIC X(5).
01 WS-VAR2 REDEFINES WS-VAR1 PIC 9(3).

According to my understanding the value of WS-VAR1 will be ' ABC' and the value of WS-VAR2 is 123.

Please correct if i am wrong . Parallely i will try testing it .

thanks,
L.nethaji
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


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

PostPosted: Fri Jan 08, 2010 11:15 pm    Post subject:
Reply with quote

Mr. L.nethaji,

you seem to have a real problem with the REDEFINE clause used in COBOL.

redefines tells the compiler to map the memory differently than just previously defined.

WS-VAR1 is a 5 byte area.
WS-VAR2 is the first three bytes of WS-VAR1.

therefore, whatever is in one, is by definition in the other.

you really don't have to test anything. Read any COBOL Programmers Application Guide.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Fri Jan 08, 2010 11:37 pm    Post subject:
Reply with quote

Quote:
If ws-var1 has value 'ABC' and Ws-var2 had 123 .
This is impossible if WS-VAR2 redefines WS-VAR1. The REDEFINES keyword means the two variable share the exact same memory location -- so there is no chance, under any circumstances whatsoever, that the two variables would have different values -- unless they are different lengths, in which the two variables would be exactly the same up to the length of the shorter one.

Read the manuals.

Read the manuals.

READ THE MANUALS!
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Fri Jan 08, 2010 11:55 pm    Post subject: Hi
Reply with quote

I missed to say that i just want to know if they are of same size

01 WS-VAR1 PIC X(3) value 'ABC'
01 WS-VAR2 REDEFINES WS-VAR1 PIC 9(3).

If ws-var1 has a initial value of ABC.then,the value of ws-var2 will be ABC .

In procedure division ,i am moving 123 to WS-VAR2.

In this condition the value of WS-VAR1 will be ABC and the value is WS-VAR2 is 123.

This is wat i meant to explain.
I just missed a lot in my previous post. Pls help if i am wrong based on this scenario.

Thanks for correcting me
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Sat Jan 09, 2010 12:03 am    Post subject:
Reply with quote

Since you're not really paying attention to what you are being told, here is actual, tested COBOL code:
Code:
          05  WS-VAR1                 PIC X(03) VALUE 'ABC'.
          05  WS-VAR2                 REDEFINES WS-VAR1
                                      PIC 9(03).
     *
      PROCEDURE DIVISION.
      S1000-INITIALIZE.
          DISPLAY 'WS-VAR1 BEFORE MOVE ' WS-VAR1.
          DISPLAY 'WS-VAR2 BEFORE MOVE ' WS-VAR2.
          MOVE 123                    TO  WS-VAR2.
          DISPLAY 'WS-VAR1 AFTER  MOVE ' WS-VAR1.
          DISPLAY 'WS-VAR2 AFTER  MOVE ' WS-VAR2.
which produces actual output of:
Code:
 WS-VAR1 BEFORE MOVE ABC
 WS-VAR2 BEFORE MOVE ABC
 WS-VAR1 AFTER  MOVE 123
 WS-VAR2 AFTER  MOVE 123
As this example shows, what you are saying is wrong:
Quote:
In procedure division ,i am moving 123 to WS-VAR2.

In this condition the value of WS-VAR1 will be ABC and the value is WS-VAR2 is 123.
As I said in my earlier post, you literally cannot have the two variables having different values -- no matter what you do.
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Sat Jan 09, 2010 12:15 am    Post subject: Hi ROBERT
Reply with quote

MAy be the way i explain things is not clear. Try to do my best next time.


YA i understand it well now.

" u meant to say the values are going to be the same for 2 variables if we redefine one with the other."


Thanks for the reply .
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Sat Jan 09, 2010 12:26 am    Post subject:
Reply with quote

Just to cover all the bases, this code:
Code:
           05  WS-VAR1                 PIC X(05) VALUE 'ABCDE'.
           05  WS-VAR2                 REDEFINES WS-VAR1
                                       PIC 9(03).
      *
       PROCEDURE DIVISION.
       S1000-INITIALIZE.
           DISPLAY 'WS-VAR1 BEFORE MOVE ' WS-VAR1.
           DISPLAY 'WS-VAR2 BEFORE MOVE ' WS-VAR2.
           MOVE 123                    TO  WS-VAR2.
           DISPLAY 'WS-VAR1 AFTER  MOVE ' WS-VAR1.
           DISPLAY 'WS-VAR2 AFTER  MOVE ' WS-VAR2.
produces as output
Code:
 WS-VAR1 BEFORE MOVE ABCDE
 WS-VAR2 BEFORE MOVE ABC
 WS-VAR1 AFTER  MOVE 123DE
 WS-VAR2 AFTER  MOVE 123
Back to top
View user's profile Send private message
l.nethaji

New User


Joined: 16 Mar 2008
Posts: 90
Location: tamil nadu

PostPosted: Sat Jan 09, 2010 12:39 am    Post subject: Thanks for this Examples
Reply with quote

Hi,

On reading i am not able to get actually wat redefines does .
But i got it from ur examples .

Thanks for ur explaination on different cases.

Sure i got some new concepts learnt from my errors.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Sat Jan 09, 2010 12:44 am    Post subject:
Reply with quote

Glad to hear you've got it!
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
This topic is locked: you cannot edit posts or make replies. RANDOM Function in COBOL swapnil781 COBOL Programming 2 Tue Nov 15, 2016 6:17 pm
No new posts Regarding COBOL Stored Procedure opti... selvamsrinivasan85 DB2 4 Fri Nov 04, 2016 8:57 pm
No new posts COBOL Version 6.1 Virendra Shambharkar COBOL Programming 5 Tue Nov 01, 2016 11:24 am
No new posts Cobol list of programs being called biswajit.dattagupta COBOL Programming 5 Tue Nov 01, 2016 2:10 am
No new posts Can sending 5 MB data between cobol p... Kevin Vaz CICS 12 Tue Oct 18, 2016 4:50 pm


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