Displaying COMP fields



Displaying COMP fields

Postby raghuvanshi » Sat Oct 27, 2012 7:41 pm

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.
raghuvanshi
 
Posts: 43
Joined: Tue Dec 07, 2010 5:32 pm
Has thanked: 3 times
Been thanked: 0 time

Re: Displaying COMP fields

Postby BillyBoyo » Sat Oct 27, 2012 7:45 pm

Well, your interviewer doesnt' know what they're talking about.

I would define as an "edited" field with a sign, not a separate sign.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Displaying COMP fields

Postby Robert Sample » Sat Oct 27, 2012 8:09 pm

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.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Displaying COMP fields

Postby sinmani » Tue Apr 23, 2013 4:15 pm

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??
-----------------------------------------
As long as you think you are a student, you are doing well.
The day you consider yourself as the master of the game..........well
sinmani
 
Posts: 93
Joined: Thu Mar 22, 2012 10:02 am
Has thanked: 14 times
Been thanked: 0 time

Re: Displaying COMP fields

Postby Robert Sample » Tue Apr 23, 2013 4:37 pm

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 ********************************
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Displaying COMP fields

Postby sinmani » Tue Apr 23, 2013 5:21 pm

Hi Robert,

Sorry about the misplaced Pic clause which caused compilation error. :roll:
But what could be the reason that it is not working?? :?:
-----------------------------------------
As long as you think you are a student, you are doing well.
The day you consider yourself as the master of the game..........well
sinmani
 
Posts: 93
Joined: Thu Mar 22, 2012 10:02 am
Has thanked: 14 times
Been thanked: 0 time

Re: Displaying COMP fields

Postby Robert Sample » Tue Apr 23, 2013 5:35 pm

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.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Displaying COMP fields

Postby BillyBoyo » Tue Apr 23, 2013 6:37 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Re: Displaying COMP fields

Postby sinmani » Wed Apr 24, 2013 11:28 am

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.
-----------------------------------------
As long as you think you are a student, you are doing well.
The day you consider yourself as the master of the game..........well
sinmani
 
Posts: 93
Joined: Thu Mar 22, 2012 10:02 am
Has thanked: 14 times
Been thanked: 0 time

Re: Displaying COMP fields

Postby BillyBoyo » Wed Apr 24, 2013 12:56 pm

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.
BillyBoyo
Global moderator
 
Posts: 3804
Joined: Tue Jan 25, 2011 12:02 am
Has thanked: 22 times
Been thanked: 265 times

Next

Return to Interview Questions