Exemple – Classification d’un tableau

Voici un programme utilisant un tableau :

IDENTIFICATION DIVISION. 
PROGRAM-ID.    SORTTABL.
AUTHOR.        LA COMMUNAUTE DU COBOL.
DATA DIVISION. 
WORKING-STORAGE SECTION. 

01  WS-TABLE                         VALUE ZEROES. 
    05 WS-ELEMENT    PIC 9(002) OCCURS 1 TO 99 
                                          DEPENDING WS-ELEM.
01  WS-AUXILIARES. 
    05  WS-ELEM       PIC 9(002)      VALUE 15. 
    05  I             PIC 9(002)      VALUE 1. 
    05  J             PIC 9(002). 
    05  K             PIC 9(002). 
    05  T             PIC X(002). 

PROCEDURE DIVISION. 

    PERFORM 001-INIT
    PERFORM 002-PROCESS
    PERFORM 003-FINAL
    STOP RUN.

001-INIT. 

    MOVE 102204521253650667569970013345 TO WS-TABLE.

002-PROCESS.

    PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-ELEM 
            COMPUTE K = I + 1 
            PERFORM VARYING J FROM K BY 1 UNTIL J > WS-ELEM 
                    IF  WS-ELEMENT (I) > WS-ELEMENT (J) 
                        MOVE WS-ELEMENT (I) TO T  
                        MOVE WS-ELEMENT (J) TO WS-ELEMENT (I) 
                        MOVE T TO WS-ELEMENT (J)
                    END-IF 
            END-PERFORM 
    END-PERFORM. 

003-FINAL.

    MOVE 1 TO I. 
    DISPLAY 'La table triée :'. 
    PERFORM WS-ELEM TIMES 
            DISPLAY WS-ELEMENT(I) 
            ADD 1 TO I  
    END-PERFORM.   

    DISPLAY 'La table triée - 1: '. 
    DISPLAY WS-TABELEMT.   
La table triée :

01
04
06
10
12
22
33
45
52
53
56
65
67
70
99

La table triée - 1:

010406101222334552535665677099