SEARCH

Syntax SEARCH (array$, tofind$, start, compare [,row] )
Location ARRAY

The function SEARCH searches in a two or three-dimensional string array array$ for the string tofind$. The search is not case-sensitive but nevertheless very fast (as the example shows). SEARCH will always look at one row only - there is just one if the array is two-dimensional but for three-dimensional string arrays (where there are in fact two-dimensions of strings because the third dimension is the maximum string length) the optional row parameter which defaults to the first row can be used to select a certain row. The start parameter allows you to tell SEARCH from which element in the row onwards it should look (remember that the first element is indexed with 0). Compare specifies the number of characters at the start of each entry to ignore, so 0 will search the whole entry for tofind$. The search stops if tofind$ was found in an entry but not if the entry and tofind$ are identical.

SEARCH returns the entry index or -1 if no suitable entry was found.

Example

Lines 100 to 170 of the following example initialise the name$ array with n (here 1000) random strings of varying length, from four to 10 characters; this can take a while. After that, the whole array is scanned for the string QL and all occurrences are listed. If you want to check out the tremendous speed of SEARCH, amend line 100, set n to 10000 and assure that at least 100K of memory is free for the huge array: you will be surprised, even the 10000 entries are searched in next to no time!

100 n = 1000: DIM name$(n,10)
110 FOR i = 1 TO n
120   name$(i) = ""
130   FOR j = 1 TO 10
140     name$(i) = name$(i) & CHR$(RND(65 TO 90))
150     IF j > 3 AND NOT RND(5) THEN EXIT j
160   END FOR j
170 END FOR i
180 :
190 first = 1
200 REPeat loop
210   found = SEARCH(name$, "QL", first, 0)
220   IF found < 0 THEN EXIT loop
230   PRINT name$(found)
240   IF found = n THEN EXIT loop: ELSE first = found + 1
250 END REPeat loop

Minerva and SMS users can use integers for n, i, j, first and found to speed up things, so replace them by n%, i%, j%, first% and found%.

CROSS-REFERENCE

Use INSTR to locate a sub-string in a string. INARRAY% is similar. See the other implementation of SEARCH.