COBOL - Exemplos simples - SEARCH - TABELA DESORDENADA - CHAVE ALFA



Desenvolvido por DORNELLES Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

COBOL - Exemplos simples - SEARCH - TABELA DESORDENADA - CHAVE ALFA

      *-----------------------------------------------------------------
       IDENTIFICATION DIVISION.                                         
      *-----------------------------------------------------------------
       PROGRAM-ID.       SEARCH.                                      
       AUTHOR.           DORNELLES CARLOS ALBERTO.                      
      *-----------------------------------------------------------------
      *  PROGRAMA      : SEARCH                                       
      *  OBJETIVO      : USO DO COMANDO SEARCH COM TABELA DESORDENADA
      *                : CHAVE ALFA - PODE SER NUMERICA           
      *  ANALISTA      : CARLOS ALBERTO DORNELLES                       
      *  DESENVOLVEDOR : CARLOS ALBERTO DORNELLES                       
      *  LINGUAGEM     : COBOL                                          
      *  MODO OPERACAO : BATCH                                          
      *-----------------------------------------------------------------
      *  VERSAO DD.MM.AAAA  HISTORICO        AUTOR                             
      *  ------ ----------  ---------------  ---------------------------                           
      *    001  20.06.2021  PROGRAMA INICIAL DORNELLES CARLOS ALBERTO                           
      *-----------------------------------------------------------------

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

       DATA DIVISION.

       WORKING-STORAGE SECTION.                                         

       01  WS-TABELA-ESTADOS.                                           
           05 TAB-NOME-UF.                                              
              10  FILLER       PIC X(021) VALUE "CECEARA              ".
              10  FILLER       PIC X(021) VALUE "DFDISTRITO FEDERAL   ".
              10  FILLER       PIC X(021) VALUE "SPSAO PAULO          ".
              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 "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 "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 "MGMINAS GERAIS       ".
              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 "RNRIO GRANDE DO NORTE".
              10  FILLER       PIC X(021) VALUE "RORONDONIA           ".
              10  FILLER       PIC X(021) VALUE "RRRORAIAMA           ".
              10  FILLER       PIC X(021) VALUE "RJRIO DE JANEIRO     ".
              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 "TOTOCANTINS          ".
           05 TAB-NOME-UF-R    REDEFINES  TAB-NOME-UF.                  
              07  WS-CAJUGUDU  OCCURS 27 TIMES                          
                               ASCENDING KEY SIGLA-UF                  
                               INDEXED BY IX-TAB.                       
                  10  SIGLA-UF PIC X(002).                              
                  10  NOME-UF  PIC X(019).                              

       01  WS-AUXILIARES.                                               
           05 NAO-ACHOU-NADA             PIC 9(003) VALUE ZEROES.       
           05 WS-SIGLA-UF                PIC X(002) VALUE SPACES.       
           05 WS-VARIAVEL                PIC X(019) VALUE SPACES.       

       PROCEDURE DIVISION.                                              

           MOVE ZEROES TO NAO-ACHOU-NADA                                
           MOVE "MT"   TO WS-SIGLA-UF
           SET IX-TAB TO 1                                              
           SEARCH WS-CAJUGUDU                                           
                  AT END                                                
                     MOVE 1 TO NAO-ACHOU-NADA                           
                WHEN SIGLA-UF (IX-TAB) EQUAL WS-SIGLA-UF                
                     MOVE NOME-UF (IX-TAB) TO WS-VARIAVEL                           
           END-SEARCH                                                   

           IF  NAO-ACHOU-NADA EQUAL 1
               DISPLAY "SIGLA - " WS-SIGLA-UF " NAO ENCONTRADA"
           ELSE
               DISPLAY "SIGLA - " WS-SIGLA-UF " - "
                        WS-VARIAVEL " ENCONTRADA"
           END-IF 
           STOP RUN.

           SIGLA - MT - MATO GROSSO         ENCONTRADA

           MOVE "FN"   TO WS-SIGLA-UF
           SIGLA - FN NAO ENCONTRADA