2 search - search for an element satisfying a specified condition
5 search(a, b [, [c] [, [d] ] ])
8 a matrix, list, association or file
9 b string if a is a file, otherwise any
10 c integer, defaults to zero or current file-position
11 d integer, defaults to size(a) or current file-position
13 return nonnegative integer or null value
17 Negative values of c and nonpositive values for d are treated as
18 offsets from size(a), i.e. as if c were replaced by size(a) + c,
19 and d by size(a) + d. Any such adjustment is assumed in the following
25 For a matrix, list, or association a,
26 search(a, b, c, d) returns, if it exists, the least index i for which
27 c <= i < d, 0 <= i < size(a), and, if accept() has not been defined,
28 a[[i]] == b, or if accept() has been defined, accept(a[[i]], b)
29 tests as nonzero. The null value is returned if there is no such i.
31 For example, to search for the first a[[i]] > b an appropriate
32 accept() function is given by:
34 define accept(v,b) = (v > b);
36 To restore the original behavior of search(), one may then use
38 define accept(v, b) = (v == b).
40 Since the addresses (rather than values) of a and b are passed,
41 the values of v = x[[i]] and b may be changed during execution
42 of search(a, b, c, d), e.g. if accept(v,b) has been defined by
44 define accept(v,b) = (v > b ? v-- : b++);
47 For a is a file-stream:
49 c defaults to the current file-position if there are just two
50 arguments (a,b) or if there are four arguments as in (a,b, ,d)
51 where d is an integer. Otherwise c defaults to zero.
53 d defaults to the current file-position or size(a) according as
54 the number of arguments (indicated by commas) is four or less
57 If a is a file, a string formed by n successive characters in a
58 is considered to occur at the file position
59 of the first character. E.g. if a has the characters "123456",
60 the string "345" is said to occur at position 2.
62 The file is searched forwards from file-position pos = c for
63 a match with b (not including the terminating '\0').
64 Only characters with file-positions less than d are considered,
65 so the effective interval for the first-character position pos
66 for a matching string is limited by both c <= pos <= d - strlen(b)
67 and 0 <= pos < size(a) - strlen(b).
69 The function returns pos if a match is found, and the reading position
70 for the stream after the search will then correspond to the position of
71 the terminating '\0' for the string b.
73 The null value is returned if no match is found. If c, d, size(a)
74 and strlen(b) are such that no match is possible, no reading of the
75 file occurs and the current file-position is not changed. In a case
76 where characters are read, the final file-position will be
77 min(d, size(a)) - strlen(b) + 1,
78 i.e. the file will be at the first position where a match is impossible
79 because the specified search region has insufficient remaining characters.
82 > L = list(2,"three",4i)
90 > f = fopen("foo", "w+")
91 > fputs(f, "This file has 28 characters.")
102 > search(f, "ha", 12)
104 > search(f, "ha", -10)
108 > search(f, "ha", 11, 19)
111 > search(f, "ha", 11, 20)
113 > search(f, "ha", 5, 500)
123 assoc, list, mat, rsearch