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.
Displaying COMP fields
-
- Posts: 43
- Joined: Tue Dec 07, 2010 5:32 pm
- Skillset: COBOL,JCL,DB2,VSAM,CA7
- Referer: Google
-
- Global moderator
- Posts: 3805
- Joined: Tue Jan 25, 2011 12:02 am
- Skillset: Easytrieve Plus, Cobol, Utilities, that sort of stuff
- Referer: Google
Re: Displaying COMP fields
Well, your interviewer doesnt' know what they're talking about.
I would define as an "edited" field with a sign, not a separate sign.
I would define as an "edited" field with a sign, not a separate sign.
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: Displaying COMP fields
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.
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.
-
- Posts: 93
- Joined: Thu Mar 22, 2012 10:02 am
- Skillset: COBOL
CICS
JCL
DB2
VSAM
IMS - Referer: INTERNET
Re: Displaying COMP fields
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??
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
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
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: Displaying COMP fields
sinmani, what your first rule on this -- or any -- forum should be is to post code that works. Your code generates a compiler error: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:produces results of
Code: Select all
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.
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:
Code: Select all
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.
Code: Select all
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+---->
VAR1 >0001<
4ECDF46FFFF4
051910E0001C
------------------------------------------------------------------------------
VAR2 >....<
4ECDF4600004
051920E0100C
------------------------------------------------------------------------------
VAR2 NOT NUMERIC
4ECDF4DDE4DEDCDCC
05192056305445993
------------------------------------------------------------------------------
******************************** Bottom of Data ********************************
-
- Posts: 93
- Joined: Thu Mar 22, 2012 10:02 am
- Skillset: COBOL
CICS
JCL
DB2
VSAM
IMS - Referer: INTERNET
Re: Displaying COMP fields
Hi Robert,
Sorry about the misplaced Pic clause which caused compilation error.
But what could be the reason that it is not working??
Sorry about the misplaced Pic clause which caused compilation error.

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
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
-
- Global moderator
- Posts: 3720
- Joined: Sat Dec 19, 2009 8:32 pm
- Skillset: Systems programming, SAS, COBOL, CICS, JCL, SMS, VSAM, etc.
- Referer: other forum
- Location: Dubuque, Iowa, USA
Re: Displaying COMP fields
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.
-
- Global moderator
- Posts: 3805
- Joined: Tue Jan 25, 2011 12:02 am
- Skillset: Easytrieve Plus, Cobol, Utilities, that sort of stuff
- Referer: Google
Re: Displaying COMP fields
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.
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.
-
- Posts: 93
- Joined: Thu Mar 22, 2012 10:02 am
- Skillset: COBOL
CICS
JCL
DB2
VSAM
IMS - Referer: INTERNET
Re: Displaying COMP fields
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
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
-
- Global moderator
- Posts: 3805
- Joined: Tue Jan 25, 2011 12:02 am
- Skillset: Easytrieve Plus, Cobol, Utilities, that sort of stuff
- Referer: Google
Re: Displaying COMP fields
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.
-
- Similar Topics
- Replies
- Views
- Last post
-
- 3
- 5887
-
by sergeyken
View the latest post
Sat Dec 10, 2022 2:30 am
-
- 5
- 2724
-
by sergeyken
View the latest post
Thu Feb 25, 2021 8:19 pm
-
- 6
- 2696
-
by Pedro
View the latest post
Sun Nov 14, 2021 11:28 am
-
-
Moving High-Values from Comp-3 - COBOL Ver 6.4
by girik1001 » Thu Jul 20, 2023 12:08 pm » in IBM Cobol - 1
- 3918
-
by sergeyken
View the latest post
Thu Jul 20, 2023 6:38 pm
-
-
-
how to extract a portion (e.g. fields 10 - 20) of a record
by ISPFTSOJCL » Fri Feb 26, 2021 5:33 am » in JCL - 2
- 1692
-
by enrico-sorichetti
View the latest post
Fri Feb 26, 2021 2:37 pm
-