Array processing and Table handling with packed decimal



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Array processing and Table handling with packed decimal

Postby rogerstrycova » Tue Oct 26, 2021 3:55 pm

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?
rogerstrycova
 
Posts: 6
Joined: Tue Sep 14, 2021 8:32 pm
Has thanked: 0 time
Been thanked: 0 time

Re: Array processing and Table handling with packed decimal

Postby sergeyken » Wed Oct 27, 2021 12:39 am

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.
Javas and Pythons come and go, but JCL and SORT stay forever.
User avatar
sergeyken
 
Posts: 285
Joined: Wed Jul 24, 2019 10:12 pm
Has thanked: 5 times
Been thanked: 25 times

Re: Array processing and Table handling with packed decimal

Postby Robert Sample » Wed Oct 27, 2021 1:12 am

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


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post