-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path548.html
More file actions
82 lines (73 loc) · 3.27 KB
/
Copy path548.html
File metadata and controls
82 lines (73 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<h1 id="search">SEARCH</h1>
<blockquote>
<p>SEARCH A, key, BYREF idx [USE cmpfunc( var1, var2)]</p>
</blockquote>
<p>Scans an array <code>A</code> for the key <code>key</code> and
returns the position <code>idx</code> of the element. If the key is not
found <code>idx</code> contains the value <code>(LBOUND(A)-1)</code>. In
default-base arrays (starting with element zero) <code>-1</code> will be
returned.</p>
<p>The optional compare function <code>cmpfunc</code> takes 2 vars
<code>var1</code> and <code>var2</code>. <code>var1</code> contains the
value of the actuell element and <code>var2</code> is equal to
<code>key</code>. The compare function must return <code>0</code> or
<code>1</code> (<code>false</code> or <code>true</code>). When it
returns <code>1</code>, the search will stop and the current element
position is returned in <code>idx</code>.</p>
<h3 id="example-1-1d-array-of-numbers">Example 1: 1D array of
numbers</h3>
<pre><code>option base 1
A = [1,9,6,4,5,3,7,8,2] ' 1D array with 9 elements
SEARCH A, 3, ElementID
print "Element: "; ElementID ' Output: Element: 6</code></pre>
<h3 id="example-2-2d-array-of-numbers">Example 2: 2D array of
numbers</h3>
<pre><code>option base 1
A = [1,9,6;4,5,3;7,8,2] ' 2D matrix with 3x3 elements
SEARCH A, 3, ElementID
print "Element: "; ElementID ' Output: Element: 6</code></pre>
<h3
id="example-3-get-the-element-position-of-the-maximum-value-of-an-1d-array">Example
3: Get the element position of the maximum value of an 1D array</h3>
<pre><code>option base 1
A = [1,9,6,4,5,3,7,8,2]
m = max(A)
SEARCH A, m, ElementID
PRINT "Element: "; ElementID ' Output: Element: 2</code></pre>
<h3 id="example-4-1d-array-of-strings">Example 4: 1D array of
strings</h3>
<pre><code>option base 1
A = ["car", "dog", "house", "paper"]
SEARCH A, "house", ElementID
PRINT "Element: "; ElementID ' Output: Element: 3</code></pre>
<h3
id="example-5-compare-function-find-first-element-greater-than-the-key">Example
5: Compare function, find first element greater than the key</h3>
<pre><code>option base 1
func findgreater(x,y)
findgreater = !(x > y)
end
A = [1,2,3,4,5,6,7,8,9]
search A, 5, ElementID USE findgreater(x,y)
PRINT "Element: "; ElementID ' Output: Element: 6</code></pre>
<h3
id="example-6-compare-function-find-first-element-which-can-be-divided-by-the-key">Example
6: Compare function, find first element which can be divided by the
key</h3>
<pre><code>option base 1
func FindFirstDivider(x,y)
FindFirstDivider = ( (x mod y) > 0 )
end
A = [1,5,7,4,5,6,7,8,9]
search A, 3, ElementID USE FindFirstDivider(x,y)
PRINT "Element: "; ElementID ' Output: Element: 6</code></pre>
<h3
id="example-7-compare-function-find-first-element-with-string-length-given-by-the-key">Example
7: Compare function, find first element with string length given by the
key</h3>
<pre><code>func FindFirstStringWithLenght(x,y)
FindFirstStringWithLenght = ( len(x) != y )
end
A = ["car", "dog", "house", "paper"]
SEARCH A, 5, ElementID use FindFirstStringWithLenght(x,y)
PRINT "Element: "; elementID ' Output: Element: 3</code></pre>