Programmers are always looking for ways to make their programs more "friendly," easier to use.
This special search routine will accept all kinds of wrong input and still come up with the right match. For VIC, PET, and 64.
Have you ever searched through a file for something but just couldn't find it? You know it's in there, but your spelling may be off by one letter and the strings just won't match?
Or you know the city of Albuquerque is in the program, but you can't spell it? Or you don't know if you're supposed to add the state? And if you do need to type the state, should you use the two-letter abbreviation? Is New Mexico supposed to be NE or NM?
In short, if a program has to search for a string match, you can solve all these problems by adding a Truncating Search Routine.
Let's look at the example program. Here a user enters the name of a city, and the program gives the elevation. If no match is found for the user's request, rather than having line 120 report "CITY NOT FOUND": GOTO 70, the program performs a truncating search (lines 160 to 210).
The routine searches only that first part of each City string equal to the length of the Request string. If there is no match, it shortens the end of the Request string by one letter and searches the shorter portion of each City string. It will continue to shorten and search until it finds a match or runs down to two letters. It will print all matches found for that length Request string.
Suppose the user gets the two-letter abbreviation of Maine wrong. If the user requests PORTLAND MA rather than ME or types out the complete word "MAINE", it will still find PORTLAND ME. If the user requests just PORTLAND, the search will print both PORTLANDs. As for our Albuquerque problem, the word can be badly misspelled and still be found. A user who understands the Truncating Search would just enter ALBU. It's a very handy and user-friendly routine, especially for poor spellers.
Data Searcher Demonstration Program
10 REM PICK CITY - PRINT CITY AND ELEVATI
20 NUMBER OF CITIES=5
30 DIM CITY$(NUMBER OF CITIES),ELEV$(NUMB
ER OF CITIES)
40 FOR I=1 TO NUMBER OF CITIES
50 READ CITY$(I),ELEV$(I)
70 T=0:PRINT"ENTER CITY NAME"
80 INPUT REQUEST$
90 FOR I=1 TO NUMBER OF CITIES
100 IF REQUEST$=CITY$(I) THEN PRINT CITY$(
120 REM NOTHING FOUND
130 REM SEARCH SIMILAR SPELLING
140 REM ==========================
150 PRINT"SEARCHING FOR SOMETHING SIMILAR
160 FOR Z=LEN(REQUEST$) TO 2 STEP -1
170 FOR I=1 TO NUMBER OF CITIES
180 IF LEFT$(REQUEST$,Z)=LEFT$(CITY$(I),Z)
THEN PRINT CITY$(I),ELEV$(I):T=1
190 NEXT I
200 IF T THEN 70
210 NEXT Z
220 PRINT"CITY NOT FOUND":GOTO 70
230 REM DATA
240 REM 8888888888
250 DATA ALBUQUERQUE NM,4500
260 DATA BISHOP CA,4100
270 DATA PORTLAND MA,45
280 DATA PORTLAND OR,37
290 DATA THE DALLES OR,85