|
1 | 1 | # SEARCH |
2 | 2 |
|
3 | | -> SEARCH A, key, BYREF ridx [USE cmpfunc] |
| 3 | +> SEARCH A, key, BYREF idx [USE cmpfunc( var1, var2)] |
4 | 4 |
|
5 | | -Scans an array for the key. If key is not found the SEARCH command returns (in ridx) the value. (LBOUND(A)-1). In default-base arrays that means -1. The cmpfunc (if its specified) it takes 2 vars to compare. It must return 0 if x = y; non-zero if x <> y. |
| 5 | +Scans an array `A` for the key `key` and returns the position `idx` of the element. If the key is not found `idx` contains the value `(LBOUND(A)-1)`. In default-base arrays (starting with element zero) `-1` will be returned. |
| 6 | + |
| 7 | +The optional compare function `cmpfunc` takes 2 vars `var1` and `var2`. `var1` contains the value of the actuell element and `var2` is equal to `key`. The compare function must return `0` or `1` (`false` or `true`). When it returns `1`, the search will stop and the current element position is returned in `idx`. |
| 8 | + |
| 9 | +### Example 1: 1D array of numbers |
| 10 | + |
| 11 | +``` |
| 12 | +option base 1 |
| 13 | +
|
| 14 | +A = [1,9,6,4,5,3,7,8,2] ' 1D array with 9 elements |
| 15 | +SEARCH A, 3, ElementID |
| 16 | +print "Element: "; ElementID ' Output: Element: 6 |
| 17 | +``` |
| 18 | + |
| 19 | +### Example 2: 2D array of numbers |
| 20 | + |
| 21 | +``` |
| 22 | +option base 1 |
| 23 | +
|
| 24 | +A = [1,9,6;4,5,3;7,8,2] ' 2D matrix with 3x3 elements |
| 25 | +SEARCH A, 3, ElementID |
| 26 | +print "Element: "; ElementID ' Output: Element: 6 |
| 27 | +``` |
| 28 | + |
| 29 | +### Example 3: Get the element position of the maximum value of an 1D array |
| 30 | + |
| 31 | +``` |
| 32 | +option base 1 |
| 33 | +
|
| 34 | +A = [1,9,6,4,5,3,7,8,2] |
| 35 | +m = max(A) |
| 36 | +SEARCH A, m, ElementID |
| 37 | +PRINT "Element: "; ElementID ' Output: Element: 2 |
| 38 | +``` |
| 39 | + |
| 40 | +### Example 4: 1D array of strings |
| 41 | + |
| 42 | +``` |
| 43 | +option base 1 |
| 44 | +
|
| 45 | +A = ["car", "dog", "house", "paper"] |
| 46 | +SEARCH A, "house", ElementID |
| 47 | +PRINT "Element: "; ElementID ' Output: Element: 3 |
| 48 | +``` |
| 49 | + |
| 50 | +### Example 5: Compare function, find first element greater than the key |
| 51 | + |
| 52 | +``` |
| 53 | +option base 1 |
| 54 | +
|
| 55 | +func findgreater(x,y) |
| 56 | + findgreater = !(x > y) |
| 57 | +end |
| 58 | +
|
| 59 | +A = [1,2,3,4,5,6,7,8,9] |
| 60 | +search A, 5, ElementID USE findgreater(x,y) |
| 61 | +PRINT "Element: "; ElementID ' Output: Element: 6 |
| 62 | +``` |
| 63 | + |
| 64 | +### Example 6: Compare function, find first element which can be divided by the key |
6 | 65 |
|
7 | 66 | ``` |
8 | | -FUNC cmp(x,y) |
9 | | - cmp=!(x=y) |
10 | | -END |
11 | | -... |
12 | | -DIM A(5) |
13 | | -FOR i=0 TO 5 |
14 | | - A(i)=5-i |
15 | | -NEXT |
16 | | -SEARCH A, 4, r USE cmp(x,y) |
17 | | -PRINT r:REM prints 1 |
18 | | -PRINT A(r): REM prints 4 |
| 67 | +option base 1 |
| 68 | +
|
| 69 | +func FindFirstDivider(x,y) |
| 70 | + FindFirstDivider = ( (x mod y) > 0 ) |
| 71 | +end |
| 72 | +
|
| 73 | +A = [1,5,7,4,5,6,7,8,9] |
| 74 | +search A, 3, ElementID USE FindFirstDivider(x,y) |
| 75 | +PRINT "Element: "; ElementID ' Output: Element: 6 |
19 | 76 | ``` |
20 | 77 |
|
21 | | -~~~ |
22 | | -
|
23 | | -' Note: I'm not sure yet about a practical use for [USE cmpfunc] syntax... |
24 | | -Def q(text) = Enclose(text) ' Enclose text with quotation marks, "" |
25 | | -Def rev(text) = Cat(3) + text + Cat(-3) ' reverse colors of text |
26 | | -Sub title(text) |
27 | | - ?: Color 14: Print text: Color 7 ' print title in color |
28 | | -End Sub |
29 | | -
|
30 | | -Dim a(5 To 7) |
31 | | -a(5) = "Hello" |
32 | | -a(6) = "World" |
33 | | -a(7) = 123 |
34 | | -? rev(" Dim a(5 To 7) = " + Str(a) + " ") |
35 | | -title "Use SEARCH to find index of matching element in array (Base LBOUND(a)):" |
36 | | -Search a, "Hello", i: ? "Search a, " + q("Hello") + ", i: "; i |
37 | | -Search a, "ello", i: ? "Search a, " + q("ello") + " , i: "; i |
38 | | -Search a, "HELLO", i: ? "Search a, " + q("HELLO") + ", i: "; i |
39 | | -Search a, "World", i: ? "Search a, " + q("World") + ", i: "; i |
40 | | -Search a, 123, i: ? "Search a, " + " 123 " + ", i: "; i |
41 | | -Search a, 12, i: ? "Search a, " + " 12 " + ", i: "; i |
42 | | -title "Use IN to find index of matching element in array (Always Base 1):" |
43 | | -? q("Hello") + " In a: "; "Hello" In a |
44 | | -? q("ello") + " In a: "; "ello" In a |
45 | | -? q("HELLO") + " In a: "; "HELLO" In a |
46 | | -? q("World") + " In a: "; "World" In a |
47 | | -? " 123 In a: "; 123 In a |
48 | | -? " 12 In a: "; 12 In a |
49 | | -Pause |
50 | | -
|
51 | | -~~~ |
52 | | - |
53 | | - |
54 | | -~~~ |
55 | | -
|
56 | | -Option Base 1 |
57 | | -' x is the current array element of search; |
58 | | -' y is the array element (key) that we search for. |
59 | | -Func create_new_array(x, y, Byref new_a) |
60 | | - ' Create a new array: |
61 | | - If x <> 0 Then Append new_a, x |
62 | | - |
63 | | - ' Standard expression to continue the search: |
64 | | - create_new_array = Not (x = y) |
65 | | -End Func |
66 | | -
|
67 | | -' Start demo: |
68 | | -a = ["a"; "b", "c", "d", "e"; "f"] ' Array for search |
69 | | -e = "c" ' Element (key) to find |
70 | | -Dim new_a() ' Initialize empty new array |
71 | | -Search a, e, ix Use create_new_array(x, y, new_a) |
72 | | -? "Array for search: "; a |
73 | | -? |
74 | | -? "Element (key) to find: "; e |
75 | | -? |
76 | | -' Check return-index (ix): |
77 | | -If ix = Lbound(a) - 1 Then |
78 | | - ? "Element not found in array." |
79 | | -Else |
80 | | - ? "Element found in array at index: "; ix |
81 | | -Fi |
82 | | -? |
83 | | -? "New array created on search: "; new_a |
84 | | -Pause |
85 | | -
|
86 | | -~~~ |
| 78 | +### Example 7: Compare function, find first element with string length given by the key |
| 79 | + |
| 80 | +``` |
| 81 | +func FindFirstStringWithLenght(x,y) |
| 82 | + FindFirstStringWithLenght = ( len(x) != y ) |
| 83 | +end |
| 84 | +
|
| 85 | +A = ["car", "dog", "house", "paper"] |
| 86 | +SEARCH A, 5, ElementID use FindFirstStringWithLenght(x,y) |
| 87 | +PRINT "Element: "; elementID ' Output: Element: 3 |
| 88 | +``` |
| 89 | + |
| 90 | + |
87 | 91 |
|
88 | 92 |
|
0 commit comments