Debian Codegraph @

doc/lsearch.n

raw
1 '\"
2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\" Copyright (c) 2001 Kevin B. Kenny <[email protected]>. All rights reserved.
5 '\" Copyright (c) 2003-2004 Donal K. Fellows.
6 '\"
7 '\" See the file "license.terms" for information on usage and redistribution
8 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 '\"
10 .TH lsearch n 8.6 Tcl "Tcl Built-In Commands"
11 .so man.macros
12 .BS
13 '\" Note: do not modify the .SH NAME line immediately below!
14 .SH NAME
15 lsearch \- See if a list contains a particular element
16 .SH SYNOPSIS
17 \fBlsearch \fR?\fIoptions\fR? \fIlist pattern\fR
18 .BE
19 .SH DESCRIPTION
20 .PP
21 This command searches the elements of \fIlist\fR to see if one
22 of them matches \fIpattern\fR. If so, the command returns the index
23 of the first matching element
24 (unless the options \fB\-all\fR or \fB\-inline\fR are specified.)
25 If not, the command returns \fB\-1\fR or (if options \fB\-all\fR
26 or \fB\-inline\fR are specified) the empty string. The \fIoption\fR arguments
27 indicates how the elements of the list are to be matched against
28 \fIpattern\fR and must have one of the values below:
29 .SS "MATCHING STYLE OPTIONS"
30 .PP
31 If all matching style options are omitted, the default matching style
32 is \fB\-glob\fR. If more than one matching style is specified, the
33 last matching style given takes precedence.
34 .\" OPTION: -exact
35 .TP
36 \fB\-exact\fR
37 .
38 \fIPattern\fR is a literal string that is compared for exact equality
39 against each list element.
40 .\" OPTION: -glob
41 .TP
42 \fB\-glob\fR
43 .
44 \fIPattern\fR is a glob-style pattern which is matched against each list
45 element using the same rules as the \fBstring match\fR command.
46 .\" OPTION: -regexp
47 .TP
48 \fB\-regexp\fR
49 .
50 \fIPattern\fR is treated as a regular expression and matched against
51 each list element using the rules described in the \fBre_syntax\fR
52 reference page.
53 .\" OPTION: -sorted
54 .TP
55 \fB\-sorted\fR
56 .
57 The list elements are in sorted order. If this option is specified,
58 \fBlsearch\fR will use a more efficient searching algorithm to search
59 \fIlist\fR. If no other options are specified, \fIlist\fR is assumed
60 to be sorted in increasing order, and to contain ASCII strings. This
61 option is mutually exclusive with \fB\-glob\fR and \fB\-regexp\fR, and
62 is treated exactly like \fB\-exact\fR when either \fB\-all\fR or
63 \fB\-not\fR are specified.
64 .SS "GENERAL MODIFIER OPTIONS"
65 .PP
66 These options may be given with all matching styles.
67 .\" OPTION: -all
68 .TP
69 \fB\-all\fR
70 .
71 Changes the result to be the list of all matching indices (or all matching
72 values if \fB\-inline\fR is specified as well.) If indices are returned, the
73 indices will be in ascending numeric order. If values are returned, the order
74 of the values will be the order of those values within the input \fIlist\fR.
75 .\" OPTION: -inline
76 .TP
77 \fB\-inline\fR
78 .
79 The matching value is returned instead of its index (or an empty
80 string if no value matches.) If \fB\-all\fR is also specified, then
81 the result of the command is the list of all values that matched.
82 .\" OPTION: -not
83 .TP
84 \fB\-not\fR
85 .
86 This negates the sense of the match, returning the index of the first
87 non-matching value in the list.
88 .\" OPTION: -start
89 .TP
90 \fB\-start\fR\0\fIindex\fR
91 .
92 The list is searched starting at position \fIindex\fR.
93 The interpretation of the \fIindex\fR value is the same as
94 for the command \fBstring index\fR, supporting simple index
95 arithmetic and indices relative to the end of the list.
96 .SS "CONTENTS DESCRIPTION OPTIONS"
97 .PP
98 These options describe how to interpret the items in the list being
99 searched. They are only meaningful when used with the \fB\-exact\fR
100 and \fB\-sorted\fR options. If more than one is specified, the last
101 one takes precedence. The default is \fB\-ascii\fR.
102 .\" OPTION: -ascii
103 .TP
104 \fB\-ascii\fR
105 .
106 The list elements are to be examined as Unicode strings (the name is
107 for backward-compatibility reasons.)
108 .\" OPTION: -dictionary
109 .TP
110 \fB\-dictionary\fR
111 .
112 The list elements are to be compared using dictionary-style
113 comparisons (see \fBlsort\fR for a fuller description). Note that this
114 only makes a meaningful difference from the \fB\-ascii\fR option when
115 the \fB\-sorted\fR option is given, because values are only
116 dictionary-equal when exactly equal.
117 .\" OPTION: -integer
118 .TP
119 \fB\-integer\fR
120 .
121 The list elements are to be compared as integers.
122 .\" OPTION: -nocase
123 .TP
124 \fB\-nocase\fR
125 .
126 Causes comparisons to be handled in a case-insensitive manner. Has no
127 effect if combined with the \fB\-dictionary\fR, \fB\-integer\fR, or
128 \fB\-real\fR options.
129 .\" OPTION: -real
130 .TP
131 \fB\-real\fR
132 .
133 The list elements are to be compared as floating-point values.
134 .SS "SORTED LIST OPTIONS"
135 .PP
136 These options (only meaningful with the \fB\-sorted\fR option) specify
137 how the list is sorted. If more than one is given, the last one takes
138 precedence. The default option is \fB\-increasing\fR.
139 .\" OPTION: -decreasing
140 .TP
141 \fB\-decreasing\fR
142 .
143 The list elements are sorted in decreasing order. This option is only
144 meaningful when used with \fB\-sorted\fR.
145 .\" OPTION: -increasing
146 .TP
147 \fB\-increasing\fR
148 .
149 The list elements are sorted in increasing order. This option is only
150 meaningful when used with \fB\-sorted\fR.
151 .\" OPTION: -bisect
152 .TP
153 \fB\-bisect\fR
154 .VS 8.6
155 Inexact search when the list elements are in sorted order. For an increasing
156 list the last index where the element is less than or equal to the pattern
157 is returned. For a decreasing list the last index where the element is greater
158 than or equal to the pattern is returned. If the pattern is before the first
159 element or the list is empty, -1 is returned.
160 This option implies \fB\-sorted\fR and cannot be used with either \fB\-all\fR
161 or \fB\-not\fR.
162 .VE 8.6
163 .SS "NESTED LIST OPTIONS"
164 .PP
165 These options are used to search lists of lists. They may be used
166 with any other options.
167 .\" OPTION: -index
168 .TP
169 \fB\-index\fR\0\fIindexList\fR
170 .
171 This option is designed for use when searching within nested lists.
172 The \fIindexList\fR argument gives a path of indices (much as might be
173 used with the \fBlindex\fR or \fBlset\fR commands) within each element
174 to allow the location of the term being matched against.
175 .\" OPTION: -subindices
176 .TP
177 \fB\-subindices\fR
178 .
179 If this option is given, the index result from this command (or every
180 index result when \fB\-all\fR is also specified) will be a complete
181 path (suitable for use with \fBlindex\fR or \fBlset\fR) within the
182 overall list to the term found. This option has no effect unless the
183 \fB\-index\fR is also specified, and is just a convenience short-cut.
184 .SH EXAMPLES
185 .PP
186 Basic searching:
187 .PP
188 .CS
189 \fBlsearch\fR {a b c d e} c
190 \fI\(-> 2\fR
191 \fBlsearch\fR -all {a b c a b c} c
192 \fI\(-> 2 5\fR
193 .CE
194 .PP
195 Using \fBlsearch\fR to filter lists:
196 .PP
197 .CS
198 \fBlsearch\fR -inline {a20 b35 c47} b*
199 \fI\(-> b35\fR
200 \fBlsearch\fR -inline -not {a20 b35 c47} b*
201 \fI\(-> a20\fR
202 \fBlsearch\fR -all -inline -not {a20 b35 c47} b*
203 \fI\(-> a20 c47\fR
204 \fBlsearch\fR -all -not {a20 b35 c47} b*
205 \fI\(-> 0 2\fR
206 .CE
207 .PP
208 This can even do a
209 .QW set-like
210 removal operation:
211 .PP
212 .CS
213 \fBlsearch\fR -all -inline -not -exact {a b c a d e a f g a} a
214 \fI\(-> b c d e f g\fR
215 .CE
216 .PP
217 Searching may start part-way through the list:
218 .PP
219 .CS
220 \fBlsearch\fR -start 3 {a b c a b c} c
221 \fI\(-> 5\fR
222 .CE
223 .PP
224 It is also possible to search inside elements:
225 .PP
226 .CS
227 \fBlsearch\fR -index 1 -all -inline {{abc abc} {abc bcd} {abc cde}} *bc*
228 \fI\(-> {abc abc} {abc bcd}\fR
229 .CE
230 .SH "SEE ALSO"
231 foreach(n), list(n), lappend(n), lindex(n), linsert(n), llength(n),
232 lset(n), lsort(n), lrange(n), lreplace(n),
233 string(n)
234 .SH KEYWORDS
235 binary search, linear search,
236 list, match, pattern, regular expression, search, string
237 '\" Local Variables:
238 '\" mode: nroff
239 '\" End: