IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Redefines in cobol


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
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
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: 2501
Location: Atlanta, Georgia, USA

PostPosted: Fri Jan 08, 2010 8:37 pm
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Jan 08, 2010 9:29 pm
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
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Jan 08, 2010 10:42 pm
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
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
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Fri Jan 08, 2010 11:37 pm
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
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Sat Jan 09, 2010 12:03 am
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
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Sat Jan 09, 2010 12:26 am
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
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: 8696
Location: Dubuque, Iowa, USA

PostPosted: Sat Jan 09, 2010 12:44 am
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts COBOL ZOS Web Enablement Toolkit HTTP... COBOL Programming 0
No new posts Calling DFSORT from Cobol, using OUTF... DFSORT/ICETOOL 5
No new posts Generate random number from range of ... COBOL Programming 3
Search our Forums:

Back to Top