[USflag] The American Programmer [USflag]


Home
Books on Mainframe Programming
Mainframe Manuals and Tutorials
System Abend codes, Sqlcodes, VSAM/QSAM codes
Everything about the IBM AS/400 Midrange Computer - iSeries
Everything about CICS
Cobol programs, manuals, books
  Sample Cobol code: The Simple, Single File COBOL Program
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Simple, Single File Report COBOL Program with Record Count or Final Totals
  Sample Cobol code: The Sequence Check COBOL Program
  Sample Cobol code: The Record Selection COBOL Program
  Sample Cobol code: The Edit or Validate COBOL Program
  Sample Cobol code: The The One Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Three Level Subtotal (Control Break) COBOL Program
  Sample Cobol code: The Sequential File, Batch Update COBOL Program
  Sample Cobol code: The COBOL Sort
  Sample Cobol code: The CASE Structure: COBOL EVALUATE
  Sample Cobol code: Direct Subscripting in COBOL
  Sample Cobol code: The Sequential, or Serial Search in COBOL
  Sample Cobol code: The Binary Search in COBOL
  Sample Cobol code: Loading a Table from a Sequential File in a COBOL program
  Sample Cobol code: The VSAM File Read Sequentially in a COBOL program
  Sample Cobol code: The VSAM KSDS, Read Randomly in a COBOL program
  Sample Cobol code: The VSAM File, Read Randomly in a COBOL program
  Sample Cobol code: VSAM Initial Load in a COBOL program
  Sample Cobol code: VSAM File Maintenance (Add, Change, Delete) in a COBOL program
  Sample Cobol code: VSAM Read Sequentially, with START, in a COBOL program
  Sample Cobol code: Creating a Variable Format File in a COBOL program
  Sample Cobol code: Reading a Variable Format File in a COBOL program
  Sample Cobol code: Creating a Variable Format File with Occurs Depending On in a COBOL program
  Sample Cobol code: COBOL Reading a Variable Format File with Occurs Depending On, in a COBOL program
  Sample Cobol code: COBOL The Table Load with Occurs Depending On
  Sample Cobol/DB2 code: Singleton Select
  Manuals on the COBOL programming language.
  Books on Cobol
  Abend Codes from Cobol programs
Everything about DB2 and SQL
Everything about IMS
Everything about Java and JavaScript
Everything about JCL and JES
Everything about REXX
Everything about zOS, VSAM, Tivoli, Assembler
Everything about TSO, ISPF, Spufi
Site Map and Site Search

           Home   > COBOL   > COBOL program the Sequential Search

The Sequential Search in COBOL


13. The Sequential Search

SERSRCH1.
This is a table lookup This program checks whether data exists on a table or array contained within the program, using the SEARCH verb.

Many training coordinators tell the instructor explicitly to make sure the students can handle table lookups. The best advice I can give you is master this! Insurance companies, banks, government agencies (to mention just a few) live on table lookups.

Make sure yours are coded properly - check out values at the beginning, middle and end of the table.

The sequential, or serial search is used when the entries in the table are not in order. It is probably a good choice when there are less than 100 or so entries in the table.

It would be a good idea to look up explanations of indexes and the SEARCH verb in your COBOL book

Here is the program SERSRCH1:

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.



000200 IDENTIFICATION DIVISION.
000300 PROGRAM-ID. SERSRCH1.
000400* The serial search
000500* reads every input record
000600* after looking up the employee's month of hire on a table,
000700* by a sequential search, it writes it out to an output file
000800 ENVIRONMENT DIVISION.
000900 INPUT-OUTPUT SECTION.
001000 FILE-CONTROL.
001100*    INPUT FILE EMP
001200     SELECT INPUT-FILE ASSIGN EMP.
001600*    REPORTFI: A REPORT FILE, PRINTS OUT INFORMATION ON EMPLOYEES
001700*        WITH MONTH OF HIRE, SEND TO PRINTER
001800     SELECT REPORT-FILE ASSIGN REPORTFI.
002200
002300 DATA DIVISION.
002400 FILE SECTION.
002500
002600 FD  INPUT-FILE
002700     RECORDING MODE IS F
003000     RECORD CONTAINS 80 CHARACTERS.
003100 01  INPUT-RECORD.
003120     05  FILLER                        PIC X(08).
002130     05  FILLER                        PIC X(01).
003140     05  ER-EMPLOYEE-NUMBER            PIC X(05).
003150     05  FILLER                        PIC X(01).
003160     05  ER-EMPLOYEE-NAME              PIC X(25).
003170     05  ER-EMPLOYEE-DEPARTMENT        PIC X(05).
003180     05  FILLER                        PIC X(01).
003190     05  ER-EMPLOYEE-SALARY-CODE       PIC X(02).
003200     05  FILLER                        PIC X(01).
003210*     MONTH OF HIRE CAN BE DEFINED AS CHARACTER (ALPHANUMERIC)
003230     05  ER-MONTH-OF-HIRE              PIC X(02).
003230     05  FILLER                        PIC X(29).
003230
003300 FD  REPORT-FILE
003400     RECORDING MODE IS F
003700     RECORD CONTAINS 133 CHARACTERS.
003800
003900 01  REPORT-RECORD                      PIC X(133).
004000
004100 WORKING-STORAGE SECTION.
004200
004300 01  FILE-AT-END             PIC X VALUE 'N'.
004400*
004500 01  SW-VALID-RECORD         PIC X VALUE 'Y'.
004600
006400 01  COUNTERS-AND-ACCUMULATORS.
006500     05  CTR-RECORDS-READ               PIC  S9(5)
006600            PACKED-DECIMAL       VALUE 0.
006700     05  CTR-RECORDS-WRITTEN            PIC  S9(5)
006800            PACKED-DECIMAL       VALUE 0.
006900
007000 01  TITLE-HEADING-LINE.
007100     05  FILLER                         PIC X(1) VALUE SPACES.
007200     05  FILLER                         PIC X(35)
007300                   VALUE 'EMPLOYEE RECORDS WITH MONTH OF HIRE'.
007400     05  FILLER                         PIC X(04) VALUE SPACES.
007500     05  FILLER                         PIC X(33)
007600                   VALUE SPACES.
007700     05  REPORT-DATE.
007800           10  REPORT-YY   PIC 99.
007900           10  REPORT-MM   PIC 99.
008000           10  REPORT-DD   PIC 99.
008100
008200 01  DETAIL-PRINT-LINE.
008300     05  FILLER                         PIC X(1) VALUE SPACES.
008400     05  DL-MONTH-OF-HIRE               PIC X(09).
008500     05  DL-RECORD-IMAGE                PIC X(80) VALUE SPACES.
008600
008700 01  MONTH-TABLE-LITERALS.
008800*     This is hard coding a table.
008900*     most explanations are meaningless,
009000*     until you study this carefully to see what is happening
009100*     these are fillers, because you
009200*     won't be referring to them directly, by name
009300*     notice how the code (01) is written right
009400*     beside the name (january)
009500*     all the pictures must be the same
009600*     the literals inside of quotes don't have
009700*      to be the same length, but many will code them that way
009800             05     FILLER     PIC X(11)     VALUE '01JANUARY'.
009900             05     FILLER     PIC X(11)     VALUE '02FEBRUARY'.
010000             05     FILLER     PIC X(11)     VALUE '03MARCH'.
010100             05     FILLER     PIC X(11)     VALUE '04APRIL'.
010200             05     FILLER     PIC X(11)     VALUE '05MAY'.
010300             05     FILLER     PIC X(11)     VALUE '06JUNE'.
010400             05     FILLER     PIC X(11)     VALUE '07JULY'.
010500             05     FILLER     PIC X(11)     VALUE '08AUGUST'.
010600             05     FILLER     PIC X(11)     VALUE '09SEPTEMBER'.
010700             05     FILLER     PIC X(11)     VALUE '10OCTOBER'.
010800             05     FILLER     PIC X(11)     VALUE '11NOVEMBER'.
010900             05     FILLER     PIC X(11)     VALUE '12DECEMBER'.
011000
011100*     Redefines means that this 01 level item
011200*     occupies the same spot in memory as the one it redefines
011300*     so actually the two 01 levels are
011400*     the same thing with different names and different picture
011500 01  MONTH-TABLE REDEFINES MONTH-TABLE-LITERALS.
011600*     Next item must occur as many times
011700*     as there are fillers in the preceding 01
011800*     its picture or the pictures under it
011900*     must add up to the same number as the
012000*     picture in the fillers above (11 in this example)
012100    05     EACH-MONTH-INFO     OCCURS 12 TIMES
012200*     You need the indexed by clause
012300*     if you're going to use the search verb
012400*     this defines and creates the index -
012500*     so no pictures for the index, please
012600            INDEXED BY MONTH-INDEX.
012700            10  EACH-MONTH-NUMBER           PIC XX.
012800            10  EACH-MONTH-NAME             PIC X(09).
012900
013000 PROCEDURE DIVISION.
013100     PERFORM INITIALIZATION
013200     PERFORM PROCESS-ALL UNTIL
013300        FILE-AT-END = 'Y'
013400     PERFORM TERMINATION
013500     GOBACK.
013600
013700 INITIALIZATION.
013800     OPEN INPUT INPUT-FILE
013900          OUTPUT REPORT-FILE
014000     WRITE REPORT-RECORD FROM TITLE-HEADING-LINE
014100     PERFORM READ-PAR
014200
014300     ACCEPT REPORT-DATE FROM DATE.
014400
014500 PROCESS-ALL.
014600     PERFORM LOOKUP-MONTH
014700     MOVE INPUT-RECORD TO dl-RECORD-IMAGE
014800     WRITE REPORT-RECORD FROM DETAIL-PRINT-LINE
014900
015000     PERFORM READ-PAR.
015100
015200 TERMINATION.
015300
015400     CLOSE INPUT-FILE
015500           REPORT-FILE.
015600
015700 READ-PAR.
015800     READ INPUT-FILE 
015900     AT END
016000         MOVE 'Y' TO FILE-AT-END
016100     NOT AT END
016200        ADD 1 TO CTR-RECORDS-READ
016300     END-READ.
016500 LOOKUP-MONTH.
016600*   When doing a sequential search,
016700*     you need to set the index to 1 before doing the search
016800*   or else it won't work
016900     SET MONTH-INDEX TO 1
017000*   You search the thing that occurs
017100     SEARCH EACH-MONTH-INFO
017200*   At end means not found
017300     AT END
017400*     Move 'N' to found-switch
017500*     the when is a condition, like an if.
017600*     you must "when" the thing that occurs or an item under it
017700*      comparing it to something on input record (month of hire )
017800       MOVE 'UNKNOWN' TO DL-MONTH-OF-HIRE
017900       WHEN EACH-MONTH-number(MONTH-INDEX) = ER-MONTH-OF-HIRE
018000*     Move 'Y' to found-switch
018100*     at this point you have found it - got a match
018200*     so here is where you do what you need to do on a match
018300        MOVE EACH-MONTH-NAME(month-index) TO DL-MONTH-OF-HIRE
018400        END-SEARCH.

Compliments of Gabe Gargiulo, author of several recent books on programming and modern languages, available at Amazon.com.
Here is the input data file EMP: (the next two lines are a column ruler) 1 2 3 4 5 6 123456789.123456789.123456789.123456789.123456789.123456789.12345678 01000 PEARLE E GATES D0001 01 02000 LED BALOON D0002 04 03000 KIT E LITTER D0005 06 04000 MOE PEDD D0504 01 Here is sample JCL: //STEP1 EXEC PGM=SERSRCH1 //STEPLIB DD DSN=your.executable.program.library.goes.here,DISP=SHR //*OF COURSE, THE NEXT LIBRARY NAME MAY BE DIFFERENT AT YOUR COMPANY //EMP DD DSN=userid.COBBOOK.DATA(EMP),DISP=SHR //REPORTFI DD SYSOUT=* //SYSOUT DD SYSOUT=* //SYSUDUMP DD SYSOUT=*

Top of Page

Your email and other personal information will not be given to anyone
[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |