Roger Strycova
Roger Strycova

Reputation: 109

Array processing and Table handling with packed decimal in COBOL

I was practicing array processing and table handling and there's this output of my program that I don't understand.

01  TABLE-VALUES.                      
    05 TABLE-ELEMENTS OCCURS 2 TIMES. 
       10 A-A PIC X(5).                
       10 A-B PIC S9(5)V99 COMP-3.     
01  WS-STRING PIC X(10).               
01  S PIC 99.                          
01  WS-COUNT PIC 99.                   
...
PROCEDURE DIVISION.                                       
0000-MAIN.                                                
    DISPLAY 'HELLO WORLD'                                 
    MOVE '1234567890ABCDEFGHI' TO TABLE-VALUES            
    DISPLAY 'TABLE VALUES ' TABLE-VALUES                  
    MOVE 0 TO WS-COUNT                                    
    INSPECT TABLE-VALUES TALLYING WS-COUNT FOR CHARACTERS 
    DISPLAY WS-COUNT                                      
    PERFORM 1000-PARA VARYING S FROM 1 BY 1 UNTIL S > 2  
    STOP RUN.                                             
1000-PARA.                                                
    MOVE 'A-A(&&) = ' TO WS-STRING                        
    INSPECT WS-STRING REPLACING FIRST '&&' BY S           
    DISPLAY WS-STRING A-A(S)  
    MOVE 'A-B(&&) = ' TO WS-STRING              
    INSPECT WS-STRING REPLACING FIRST '&&' BY S 
    DISPLAY WS-STRING A-B(S).                   

The output turned out to be:

HELLO WORLD                                                                     
TABLE VALUES 1234567890ABCDEFGHI                                                
18                                                                              
A-A(01) = 12345                                                                 
A-B(01) =  6 7 8                                                                
A-A(02) = 0ABCD                                                                 
A-B(02) =  5 6 7 

I don't understand how A-B(1) and A-B(2) turned out like that. Why are there spaces in between? Where did digit 9 go to?

Upvotes: 0

Views: 248

Answers (2)

Bruce Martin
Bruce Martin

Reputation: 10543

Try removing the COMP-3 from the A-B definition, it should work better.

Cobol comp-3

Comp-3 is cobol's binary coded decimal format. Each 4 bytes (1/2 byte or nyble) represents a decimal digit with the sign held in the last digit. Moving a character string to a comp-3 value (like you do) will result in a invalid comp-3 value.

Normally the value 1234 would be stored as

`01234C`x

In your case (if using an EBCDIC machine) you are moving 6789 hex string 'f6f7f8f9'x to the variable A-B(01). The F's are not valid decimal digits and 9 is not a valid comp-3 sign.

Move to Table Explanation

Explanation of

MOVE '1234567890ABCDEFGHI' TO TABLE-VALUES

in the above TABLE-VALUES is treated as a pic x(18). The move completely ignores the definitions of A-A and A-B.

Assuming a definition of

01  TABLE-VALUES.                      
    05 TABLE-ELEMENTS OCCURS 2 TIMES. 
       10 A-A PIC X(5).                
       10 A-B PIC S9(5)V99.      

The following would make more sense

MOVE 'A-A 100234567A-A 200234567' TO TABLE-VALUES

When doing a move to a group level, The string has to exactly match the map of the fields in the Group.

For the above Cobol Layout, the fields are aligned like

   Field    Position  Length
   A-A(1)      1        5
   A-B(1)      6        7
   A-A(2)      13       5
   A-B(2)      18       7

Upvotes: 3

Tom
Tom

Reputation: 21

I don’t know which compiler and directives you are using, but I would have expected the original DISPLAY of the comp-3 usage to ABEND with a numeric exception.

As for the follow up question about the letters in the numeric field, you valued the field by moving a literal to a group item, TABLE-VALUES. Group items always are USAGE ALPHANUMERIC.

Upvotes: 2

Related Questions