Monday, February 13, 2012

COBOL : Auto key generation


You might have come across situations in batch programs to auto generate keys.

Let us say we want to generate four byte keys in the following order
0001,0002,0003,…..9999,999A,……ZZZZ

The below program reads the last key generated and generates next key


WORKING-STORAGE SECTION.                                        
01 REC1.                                                        
   05 FILLER PIC X(36)                                          
      VALUE '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.             
01 REC2 REDEFINES REC1.                                         
   05 CHAR   PIC X(01) OCCURS 36 TIMES                          
                       INDEXED BY IX1, IX2, IX3, IX4.           
01 LAST-KEY  PIC X(04).                                         
01 NEXT-KEY  PIC X(04).                                         
PROCEDURE DIVISION.                                             
     MOVE 'YYYY' TO LAST-KEY.                                   
     PERFORM VARYING IX1 FROM 1 BY 1                            
             UNTIL CHAR(IX1) = LAST-KEY(1:1)                    
     END-PERFORM                                                
     PERFORM VARYING IX2 FROM 1 BY 1                            
             UNTIL CHAR(IX2) = LAST-KEY(2:1)                    
     END-PERFORM                                                
     PERFORM VARYING IX3 FROM 1 BY 1                            
             UNTIL CHAR(IX3) = LAST-KEY(3:1)                     
     END-PERFORM                                                
     PERFORM VARYING IX4 FROM 1 BY 1                            
             UNTIL CHAR(IX4) = LAST-KEY(4:1)                    
     END-PERFORM                                           
     IF IX4 = 36                                          
        SET IX4 TO 1                                      
        IF IX3 = 36                                       
           SET IX3 TO 1                                   
           IF IX2 = 36                                    
              SET IX2 TO 1                                
              IF IX1 = 36                                 
                 SET IX1 TO 1                             
              ELSE                                         
                 SET IX1 UP BY 1                          
              END-IF                                      
           ELSE                                           
              SET IX2 UP BY 1                              
           END-IF                                         
        ELSE                                              
           SET IX3 UP BY 1                                
        END-IF                                             
     ELSE                                                 
        SET IX4 UP BY 1                                   
     END-IF                                               
     MOVE CHAR(IX1) TO NEXT-KEY(1:1)                      
     MOVE CHAR(IX2) TO NEXT-KEY(2:1)   
     MOVE CHAR(IX3) TO NEXT-KEY(3:1)   
     MOVE CHAR(IX4) TO NEXT-KEY(4:1)   
     DISPLAY NEXT-KEY                  
     GOBACK.                           

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.