Page 1 of 1

Array processing and Table handling with packed decimal

PostPosted: Tue Oct 26, 2021 3:55 pm
by rogerstrycova
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?

Re: Array processing and Table handling with packed decimal

PostPosted: Wed Oct 27, 2021 12:39 am
by sergeyken
Learn about COMP-3 (in COBOL), and/or about DS PLnn (or packed decimals, in Assembler).

Without good understanding of the mentioned things it makes no sense at all to start doing what you've tried.

Re: Array processing and Table handling with packed decimal

PostPosted: Wed Oct 27, 2021 1:12 am
by Robert Sample
There are so many things wrong with your code that your best bet would be to start over and read COBOL documentation for a few months (or longer) before coding anything else.

You moved 19 bytes into TABLE-VALUES but there are only 18 bytes defined. Since COBOL processes character data left-to-right (and groups are character data), the first 18 characters got moved and the I was ignored. COBOL allocates memory for tables sequentially -- so 12345 is stored in A-A element 1. The next four bytes, 6789, are stored in A-B element 1. But since you indicated that A-B is packed decimal, I have no idea what actually got stored there. 0ABCD is then stored in A-A element 2. The next four bytes, EFGH, are stored in A-B element 2 but again I have no idea what actually got stored there. Your DISPLAY results for A-A match what I have said. The DISPLAY results for A-B are completely bogus since you violated the rules for packed decimal values in COBOL.