Exemple – SEARCH dans une table

Programme utilisant le SEARCH :

      *----------------------------------------------------
       IDENTIFICATION DIVISION.                                         
      *----------------------------------------------------
       PROGRAM-ID.       SEARCH.                                      
       AUTHOR.           LA COMMUNAUTE DU COBOL.                      
      *----------------------------------------------------
      *  PROGRAMME     : SEARCH                                       
      *  OBJECTIF      : UTILISER LA COMMANDE SEARCH DANS 
      *                : UNE TABLE          
      *----------------------------------------------------

      *----------------------------------------------------
       ENVIRONMENT DIVISION.                                            
    
       CONFIGURATION SECTION.                                           
       SPECIAL-NAMES.                                                   
               DECIMAL-POINT IS COMMA. 

       DATA DIVISION.

       WORKING-STORAGE SECTION.                                         

       01  WS-TABLE-ETATS.                                           
           05 TAB-NOM-ETAT.                                              
              10  FILLER       PIC X(021) VALUE "ACACRE               ".
              10  FILLER       PIC X(021) VALUE "ALALAGOAS            ".
              10  FILLER       PIC X(021) VALUE "AMAMAZONAS           ".
              10  FILLER       PIC X(021) VALUE "APAMAPA              ".
              10  FILLER       PIC X(021) VALUE "BABAHIA              ".
              10  FILLER       PIC X(021) VALUE "CECEARA              ".
              10  FILLER       PIC X(021) VALUE "DFDISTRITO FEDERAL   ".
              10  FILLER       PIC X(021) VALUE "ESESPIRITO SANTO     ".
              10  FILLER       PIC X(021) VALUE "GOGOIAS              ".
              10  FILLER       PIC X(021) VALUE "MAMARANHAO           ".
              10  FILLER       PIC X(021) VALUE "MGMINAS GERAIS       ".
              10  FILLER       PIC X(021) VALUE "MSMATO GROSSO DO SUL ".
              10  FILLER       PIC X(021) VALUE "MTMATO GROSSO        ".
              10  FILLER       PIC X(021) VALUE "PAPARA               ".
              10  FILLER       PIC X(021) VALUE "PBPARAIBA            ".
              10  FILLER       PIC X(021) VALUE "PEPERNAMBUCO         ".
              10  FILLER       PIC X(021) VALUE "PIPIAUI              ".
              10  FILLER       PIC X(021) VALUE "PRPARANA             ".
              10  FILLER       PIC X(021) VALUE "RJRIO DE JANEIRO     ".
              10  FILLER       PIC X(021) VALUE "RNRIO GRANDE DO NORTE".
              10  FILLER       PIC X(021) VALUE "RORONDONIA           ".
              10  FILLER       PIC X(021) VALUE "RRRORAIAMA           ".
              10  FILLER       PIC X(021) VALUE "RSRIO GRANDE DO SUL  ".
              10  FILLER       PIC X(021) VALUE "SCSANTA CATARINA     ".
              10  FILLER       PIC X(021) VALUE "SESERGIPE            ".
              10  FILLER       PIC X(021) VALUE "SPSAO PAULO          ".
              10  FILLER       PIC X(021) VALUE "TOTOCANTINS          ".
           05 TAB-NOM-ETAT-R    REDEFINES  TAB-NOM-ETAT.                  
              07  WS-ETAT      OCCURS 27 TIMES                          
                               ASCENDING KEY SIGLA-UF                  
                               INDEXED BY IX-TAB.                       
                  10  SIGLE-ETAT PIC X(002).                              
                  10  NOM-ETAT   PIC X(019).                              

       01  WS-AUXILIARES.                                               
           05 NON-TROUVE         PIC 9(003) VALUE ZEROES.       
           05 WS-SIGLE-ETAT      PIC X(002) VALUE SPACES.       
           05 WS-VARIABLE        PIC X(019) VALUE SPACES.       

       PROCEDURE DIVISION.                                              

           MOVE ZEROES TO NON-TROUVE                                
           MOVE "SP"   TO WS-SIGLE-ETAT
           SET IX-TAB TO 1                                              
           SEARCH WS-ETAT                                           
                  AT END                                                
                     MOVE 1 TO NON-TROUVE                         
                WHEN SIGLE-ETAT (IX-TAB) EQUAL WS-SIGLE-ETAT                
                     MOVE NOM-ETAT (IX-TAB) TO WS-VARIABLE                           
           END-SEARCH                                                   

           IF  NON-TROUVE  EQUAL 1
               DISPLAY "SIGLE - " WS-SIGLE-ETAT " NON TROUVE"
           ELSE
               DISPLAY "SIGLE - " WS-SIGLE-ETAT " - "
                        WS-VARIABLE " TROUVE "
           END-IF 
           STOP RUN.

           SIGLE - SP - SAO PAULO           TROUVE

           MOVE "FN"   TO WS-SIGLE-ETAT
           SIGLE - FN NON TROUVE