Page 1 of 1

COBOL Code DT041410

PostPosted: Wed Apr 14, 2010 12:36 pm
by tanmaypotdar
Hi,

Kindly lemme knw the output of below COBOL code;

01 A PIC 9 (10).
01 B REDEFINES A PIC X (10).

INITIALIZE A B.
MOVE 'TANMAY' TO B.
COMPUTE A = A + 1.
DISPLAY 'A = ' A.
DISPLAY 'B = ' B.
EXIT.

Re: COBOL Code DT041410

PostPosted: Wed Apr 14, 2010 5:11 pm
by Robert Sample
Why don't you run it yourself?

Re: COBOL Code DT041410

PostPosted: Thu Apr 15, 2010 12:03 pm
by tanmaypotdar
I ran it got abend with SOC7.
My question was;
1. Can we redefine variables with different data types?
2. If yes then why can't we perform computation on it?

Re: COBOL Code DT041410

PostPosted: Thu Apr 15, 2010 5:20 pm
by Robert Sample
1. You can redefine any type of variable as any other type of variable, and so long as the lengths match COBOL won't give you problems.
2. You need to read up in the COBOL Language Reference and Programming Guide manuals until you understand exactly what the internal formats are used by COBOL to store USAGE DISPLAY, USAGE COMP, USAGE COMP-3 data. While you can, for example, define a 4-byte USAGE DISPLAY numeric field as PIC S9(04) and then redefine a USAGE COMP-3 PIC S9(07) field on that variable (both are 4-byte fields), the fact that the internal formats are incompatible means you will almost certainly get a S0C7 abend when attempting to use the COMP-3 variable.

COBOL allows you to do things that will cause abends -- it does not protect the programmer from their own mistakes. And unless you understand the formats used by COBOL to store data, you absolutely will make mistakes.

Re: COBOL Code DT041410

PostPosted: Fri Apr 16, 2010 12:23 am
by dick scherrer
Hello and welcome to the forum,

Just curious. . .

Why would you believe that a redefinition of some PIC X field as a PIC 9 field would transform the alphanumeric value to something usable as a numeric?

Something very basic has been completely overlooked. . .

1. Can we redefine variables with different data types?
2. If yes then why can't we perform computation on it?
1. Yes, but should only do so when a data item has multiple definitions/uses. . .
2. Because computations require valid numeric values. . .

The manuals can surely help, but you would benefit by doing some basic review with your seniors or whoever was the COBOL instructor.

Re: COBOL Code DT041410

PostPosted: Fri Apr 16, 2010 1:10 pm
by tanmaypotdar
Hi,

Thanks for your prompt reply. I would definitely look for more information in manuals.

Re: COBOL Code DT041410

PostPosted: Sat Apr 17, 2010 2:26 am
by dick scherrer
Hello,

If you find something in a manual that is not clear, post what you found and your doubt about it. Someone will be able to clarify.

Good luck :)

d