Page 1 of 1

Move comp-3 to comp-3.

Posted: Mon May 28, 2018 3:23 pm
by Prakash Jha
Hi,

Can I move comp-3 variable directly to another comp-3 variable? Or I need any alphanumeric in between?

My answer was that we can move one comp-3 to another comp-3 but interview said what happen to the v in say var1 is comp3 PIC 9(4)v9(2) and var2 is PIC 9(6)v9(3)? He said what happens to v in this movement? I could not answer this, can someone please answer.

Re: Move comp-3 to comp-3.

Posted: Mon May 28, 2018 7:03 pm
by zprogrammer
You tell me what happen, I reduced your time in writing program :)

Code: Select all

       IDENTIFICATION DIVISION.                         
       PROGRAM-ID DISP01.                               
       ENVIRONMENT DIVISION.                            
       DATA DIVISION.                                   
       WORKING-STORAGE SECTION.                         
       01 A PIC 9(4)V9(2) COMP-3.                       
       01 B PIC 9(6)V9(3) COMP-3.                       
       01 C PIC 9(9)      .                             
       01 D PIC ZZZZZ9.999.                             
       PROCEDURE DIVISION.                              
       MAIN-PARA.                                       
             INITIALIZE A B C D                         
             COMPUTE A = 1234 / 100                     
             MOVE A TO B C D                            
             DISPLAY 'A= ' A                            
             DISPLAY 'B= ' B                            
             DISPLAY 'C= ' C                            
             DISPLAY 'D= ' D                            
             INITIALIZE C D                             
             MOVE B TO C D                              
             DISPLAY 'B= ' B                            
             DISPLAY 'C= ' C                            
             DISPLAY 'D= ' D                            
             STOP RUN.                                  
Output

Code: Select all

A= 001234          
B= 000012340       
C= 000000012       
D=     12.340      
B= 000012340       
C= 000000012       
D=     12.340      

Re: Move comp-3 to comp-3.

Posted: Mon May 28, 2018 8:22 pm
by Robert Sample
The V in a PICTURE clause represents an implied decimal point (implied as in the decimal point is NOT in the data). So COBOL will align the sending field decimal point and the receiving field decimal point and it will then move data based on that alignment. If the sending field has more digits to the left (or right) of the decimal point than the receiving field, truncation will occur. If the sending field has fewer digits to the left (or right) of the decimal point, zero will be used to fill the extra positions.

The Language Reference manual has a very detailed and exhaustive chart of all allowed moves in COBOL. Generally, like can be moved to like (numeric to numeric, alphanumeric to alphanumeric, national to national) but unlike (national to numeric, for example) may or may not be allowed.