Page 1 of 2

Displaying COMP fields

PostPosted: Sat Oct 27, 2012 7:41 pm
by raghuvanshi
Hi ,

Today a ques was asked to me in interview
How would you display COMP field?
And to which my reply was-:

01 Var1 PIC S9(4) COMP.
01 Var2 PIC S9(4) SIGN LEADING SEPARATE.
Move Var1 to Var2.
DISPLAY Var2

But interviewee replied that we cannot MOVE COMP field to another field.
Please give your valuable suggestion on this.

Re: Displaying COMP fields

PostPosted: Sat Oct 27, 2012 7:45 pm
by BillyBoyo
Well, your interviewer doesnt' know what they're talking about.

I would define as an "edited" field with a sign, not a separate sign.

Re: Displaying COMP fields

PostPosted: Sat Oct 27, 2012 8:09 pm
by Robert Sample
First, I would suggest that you really do not work for an organization whose interviewers are so completely clueless as to provide wrong answers to interview questions.

Second, if you check the Enterprise COBOL Language Reference manual you will find that COMP fields are considered numeric and can be moved to all sorts of variables.

Third, why not just use DISPLAY <variable name>? Why do a move at all? If the field is signed, the last character may be alphabetic instead of numeric but it is not that hard to learn how to convert A to +1, etc.

Re: Displaying COMP fields

PostPosted: Tue Apr 23, 2013 4:15 pm
by sinmani
How about this?

01 Var1 PIC S9(4) COMP.
01 Var2 PIC 9(4) REDEFINES Var1.

DISPLAY Var2

Will this work??
01 Var1 PIC S9(4) COMP will take 2 bytes.
01 Var2 PIC 9(4 will take 4 bytes.

is it possible that 01 Var2 PIC 9(4 reuses the 2 bytes of 01 Var1 plus 2 more??

Re: Displaying COMP fields

PostPosted: Tue Apr 23, 2013 4:37 pm
by Robert Sample
sinmani, what your first rule on this -- or any -- forum should be is to post code that works. Your code generates a compiler error:
     000600 01  VAR1                        PIC S9(4) COMP.
     000700 01  VAR2                        PIC 9(4) REDEFINES VAR1.


IGYDS1048-E "REDEFINES" was not the first clause in a data definition.  The clause
            was accepted.
While COBOL accepts what you posted, the E level diagnostic would prevent most sites from continuing to linkedit / bind the program.

And the answer to your question is that yes, VAR2 reuses the two bytes of VAR1 and adds 2 more bytes. However, VAR2 is not a valid numeric value -- code:
 01  VAR1                        PIC S9(4) COMP.
 01  VAR2                        REDEFINES VAR1
                                 PIC 9(4).
 PROCEDURE      DIVISION.
     MOVE +1                     TO  VAR1.
     DISPLAY 'VAR1 >' VAR1 '<'.
     DISPLAY 'VAR2 >' VAR2 '<'.
     IF  VAR2 NUMERIC
         DISPLAY 'VAR2 NUMERIC'
     ELSE
         DISPLAY 'VAR2 NOT NUMERIC'
     END-IF.
     GOBACK.
produces results of
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---->
 VAR1 >0001<
4ECDF46FFFF4
051910E0001C
 ------------------------------------------------------------------------------
 VAR2 >....<
4ECDF4600004
051920E0100C
 ------------------------------------------------------------------------------
 VAR2 NOT NUMERIC
4ECDF4DDE4DEDCDCC
05192056305445993
 ------------------------------------------------------------------------------
******************************** Bottom of Data ********************************

Re: Displaying COMP fields

PostPosted: Tue Apr 23, 2013 5:21 pm
by sinmani
Hi Robert,

Sorry about the misplaced Pic clause which caused compilation error. :roll:
But what could be the reason that it is not working?? :?:

Re: Displaying COMP fields

PostPosted: Tue Apr 23, 2013 5:35 pm
by Robert Sample
What do you think is not working? The results I displayed from the test program are EXACTLY what are expected based upon the variable formats and values.

Re: Displaying COMP fields

PostPosted: Tue Apr 23, 2013 6:37 pm
by BillyBoyo
REDEFINES is just another way of defining a piece of storage. It does not forge any relationship between the data descriptions with regard to any code in the PROCEDURE DIVISION.

DISPLAY VAR2 does not know anything about VAR1. It does not say "Oh, VAR2 is another way to look at VAR1, I'll just do a bit of rearranging and the desired answer will pop out".

The only way to make a relationship exist is by writing code. In doing that, you'd never want to use silly names like VAR1 and VAR2.

Re: Displaying COMP fields

PostPosted: Wed Apr 24, 2013 11:28 am
by sinmani
Robert Sample wrote:What do you think is not working? The results I displayed from the test program are EXACTLY what are expected based upon the variable formats and values.


+1 is stored in the first two bytes. Why doesn't it displays that in Var2.

If we move Var1 to Var2 it will display +1 in Var2.

Re: Displaying COMP fields

PostPosted: Wed Apr 24, 2013 12:56 pm
by BillyBoyo
Have a look at Robert's output. It did show you the value. Probably not how you'd expect it to. Read through the topic, read through the relevant parts of the manual, experiment, as colleagues. Post here if still unclear.