View previous topic :: View next topic
|
Author |
Message |
l.nethaji
New User
Joined: 16 Mar 2008 Posts: 90 Location: tamil nadu
|
|
|
|
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 |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
WS-VAR1 is FIVE bytes and WS-VAR2 is THREE bytes
The DISPLAY verb uses the length of the target field when performing the display.
Is this a trick question? |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
l.nethaji
New User
Joined: 16 Mar 2008 Posts: 90 Location: tamil nadu
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Glad to hear it helped! |
|
Back to top |
|
|
l.nethaji
New User
Joined: 16 Mar 2008 Posts: 90 Location: tamil nadu
|
|
|
|
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 |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
l.nethaji
New User
Joined: 16 Mar 2008 Posts: 90 Location: tamil nadu
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
l.nethaji
New User
Joined: 16 Mar 2008 Posts: 90 Location: tamil nadu
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
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 |
|
|
l.nethaji
New User
Joined: 16 Mar 2008 Posts: 90 Location: tamil nadu
|
|
|
|
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 |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Glad to hear you've got it! |
|
Back to top |
|
|
|