COBOL Indexes & SEARCH
A subscript is an ordinary numeric item that you use to pick a table element. COBOL also has indexes: special
pointers declared with the table using INDEXED BY. Indexes are faster (they hold a byte offset, not an element
number) and they are required by the SEARCH verbs.
01 WS-RATES.
05 WS-RATE-ENTRY OCCURS 5 TIMES INDEXED BY RATE-IDX.
10 WS-RATE-CODE PIC X(3).
10 WS-RATE-VALUE PIC 9V9999.Working with an index
An index is not a normal data item: you cannot MOVE to it or DISPLAY it directly. Use SET:
| Statement | Effect |
|---|---|
SET RATE-IDX TO 1 |
Point at element 1 |
SET RATE-IDX UP BY 1 |
Move to the next element |
SET RATE-IDX DOWN BY 2 |
Move back two elements |
SET WS-NUM TO RATE-IDX |
Copy the element number into a numeric item |
PERFORM VARYING works with indexes too:
IDENTIFICATION DIVISION.
PROGRAM-ID. INDEX-DEMO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COLOURS-DATA PIC X(18) VALUE "RED GREEN BLUE ".
01 WS-COLOURS REDEFINES WS-COLOURS-DATA.
05 WS-COLOUR PIC X(6) OCCURS 3 TIMES
INDEXED BY C-IDX.
01 WS-POSITION PIC 9.
PROCEDURE DIVISION.
PERFORM VARYING C-IDX FROM 1 BY 1 UNTIL C-IDX > 3
SET WS-POSITION TO C-IDX
DISPLAY WS-POSITION ": " WS-COLOUR (C-IDX)
END-PERFORM
SET C-IDX TO 3
SET C-IDX DOWN BY 1
DISPLAY "Index moved back to: " WS-COLOUR (C-IDX)
STOP RUN.1: RED
2: GREEN
3: BLUE
Index moved back to: GREEN
SEARCH: linear search
SEARCH walks through a table from the current index value until a WHEN condition is true or the table
ends. Always SET the index to 1 first:
IDENTIFICATION DIVISION.
PROGRAM-ID. LINEAR-SEARCH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-COUNTRY-DATA.
05 FILLER PIC X(14) VALUE "FRFrance".
05 FILLER PIC X(14) VALUE "JPJapan".
05 FILLER PIC X(14) VALUE "BRBrazil".
05 FILLER PIC X(14) VALUE "KEKenya".
01 WS-COUNTRIES REDEFINES WS-COUNTRY-DATA.
05 WS-COUNTRY OCCURS 4 TIMES INDEXED BY CTRY-IDX.
10 WS-CTRY-CODE PIC XX.
10 WS-CTRY-NAME PIC X(12).
01 WS-WANTED PIC XX.
PROCEDURE DIVISION.
MOVE "BR" TO WS-WANTED
PERFORM FIND-COUNTRY
MOVE "DE" TO WS-WANTED
PERFORM FIND-COUNTRY
STOP RUN.
FIND-COUNTRY.
SET CTRY-IDX TO 1
SEARCH WS-COUNTRY
AT END
DISPLAY WS-WANTED ": unknown country code"
WHEN WS-CTRY-CODE (CTRY-IDX) = WS-WANTED
DISPLAY WS-WANTED ": " WS-CTRY-NAME (CTRY-IDX)
END-SEARCH.BR: Brazil
DE: unknown country code
AT END runs when the index runs off the end of the table without a match. After a successful SEARCH, the
index points at the matching element, so you can use it afterwards.
SEARCH ALL: binary search
For large tables, SEARCH ALL does a binary search: it halves the range on each step, so 1,000 entries need
at most 10 comparisons. It has two requirements:
- The table must declare its order with
ASCENDING KEY(orDESCENDING KEY), and the data really must be in that order. - The
WHENcondition can only test the key for equality (=), possibly combined withAND.
You do not SET the index first: SEARCH ALL manages it itself.
IDENTIFICATION DIVISION.
PROGRAM-ID. BINARY-SEARCH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-PRODUCT-DATA.
05 FILLER PIC X(18) VALUE "1001Desk lamp 2499".
05 FILLER PIC X(18) VALUE "1005Chair 1450".
05 FILLER PIC X(18) VALUE "1010Notebook 0350".
05 FILLER PIC X(18) VALUE "1042Stapler 0899".
05 FILLER PIC X(18) VALUE "1100Monitor 1999".
01 WS-PRODUCTS REDEFINES WS-PRODUCT-DATA.
05 WS-PRODUCT OCCURS 5 TIMES
ASCENDING KEY IS WS-PROD-ID
INDEXED BY PROD-IDX.
10 WS-PROD-ID PIC 9(4).
10 WS-PROD-NAME PIC X(10).
10 WS-PROD-PRICE PIC 99V99.
01 WS-LOOKUP PIC 9(4).
PROCEDURE DIVISION.
MOVE 1042 TO WS-LOOKUP
PERFORM LOOKUP-PRODUCT
MOVE 1003 TO WS-LOOKUP
PERFORM LOOKUP-PRODUCT
STOP RUN.
LOOKUP-PRODUCT.
SEARCH ALL WS-PRODUCT
AT END
DISPLAY WS-LOOKUP ": no such product"
WHEN WS-PROD-ID (PROD-IDX) = WS-LOOKUP
DISPLAY WS-LOOKUP ": " WS-PROD-NAME (PROD-IDX)
" costs " WS-PROD-PRICE (PROD-IDX)
END-SEARCH.1042: Stapler costs 08.99
1003: no such product
Each FILLER literal is exactly 18 characters (4 for the id, 10 for the name, 4 for the price), so the redefined
table lines up with the data. Count the columns carefully when you build tables this way: one character too many
or too few shifts every following field.
Warning: If the data is not really sorted on the key,
SEARCH ALLcan silently miss entries that exist. The compiler cannot check this for you.
SEARCH vs SEARCH ALL
SEARCH |
SEARCH ALL |
|
|---|---|---|
| Method | Linear, from the current index | Binary |
| Table order | Any | Must be sorted on the KEY |
SET index first |
Yes | No |
WHEN conditions |
Any condition, several WHENs allowed |
One WHEN, equality on keys only |
| Best for | Small or unsorted tables | Large sorted lookup tables |
Test yourself
How do you move an index to the next element?
- ADD 1 TO IDX
- MOVE IDX + 1 TO IDX
- SET IDX UP BY 1
- INCREMENT IDX
What must you do before a serial SEARCH?
- Sort the table
- SET the index to the starting element, usually 1
- Declare ASCENDING KEY
- Nothing
What does SEARCH ALL require?
- An unsorted table
- A table declared with ASCENDING/DESCENDING KEY and data in that order
- A subscript instead of an index
- At least 100 elements