- When the SEARCH and SEARCH ALL are used, the normal table declarations are no longer sufficient. And the OCCURS clause must be extended with the INDEXED BY phrase, and in the case of a SEARCH ALL, by the KEY IS phrase. In this example, I made a copy of our 07_02 CREATETABLE program. Let's update this program to allow us to search based on SKU number. In this program, we will search the table instead of the file. I've already added two WORKING-STORAGE fields to lines 32 and 33 for the SEARCH-KEY and the resulting SEARCH-MESSAGE.
Let me scroll down a little bit. Next, we need to update our table. So on line 35, the GAMEINFO table OCCURS 10 TIMES, and I'm going to add that it is ASCENDING with a KEY IS TABLE-SKU. I also need to add INDEXED BY GAME-INDEX. Notice this GAME-INDEX variable is not defined any place else in WORKING-STORAGE.
You just need to specify it here, and then it's available in your program. Now let's scroll down to line 100. In the BEGIN paragraph, most of it's the same. On line 102, though, I did add DISPLAY 'ENTER SEARCH KEY'. We'll read that value that the user enters, which will be the four-digit SKU number, into the SEARCH-KEY variable. On line 104, I have my SEARCH statement, SEARCH ALL GAMEINFO, where GAMEINFO is the title name of our table.
AT END MOVE 'UNKNOWN' TO SEARCH-MESSAGE, that will happen if the search is unsuccessful and it can't find a matching SKU number. WHEN TABLE-SKU, which is an element name in my GAMEINFO table, and I use the GAME-INDEX variable that I just added to my GAME-TABLE definition, if that value = SEARCH-KEY. Then I'm going to move the TABLE-GAME-TITLE, using that GAME-INDEX again, TO SEARCH-MESSAGE, and then I END-SEARCH.
On line 111, I DISPLAY 'SEARCH RESULTS: ', with the SEARCH-MESSAGE. I commented out line 112, which was the PERFORM 0250-DISPLAY-DETAILS. We don't need to display the details because we want to search for certain records. Let's run the program and see how this works. So I'm going to do ./07_ 03_searchTable. When I hit enter, it says ENTER SEARCH KEY.
Let's check out Super Smash Brothers. So I'll type in 0131. The results are SUPER SMASH BROS. Cool. Now what happens if I enter in a search key that's not in my table. Let's try 1111. This is where we get the message UNKNOWN. Let me move this out of the way, and you can see on line 106, when I'm AT END of the table, it moves 'UNKNOWN' TO SEARCH-MESSAGE. Using an index makes the searching much more efficient.
Almost all programming languages use tables extensively, so being able to search a table is a really important skill to have.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings