1lsearch(n) Tcl Built-In Commands lsearch(n)
2
3
4
5______________________________________________________________________________
6
8 lsearch - See if a list contains a particular element
9
11 lsearch ?options? list pattern
12______________________________________________________________________________
13
15 This command searches the elements of list to see if one of them
16 matches pattern. If so, the command returns the index of the first
17 matching element (unless the options -all or -inline are specified.)
18 If not, the command returns -1 or (if options -all or -inline are spec‐
19 ified) the empty string. The option arguments indicates how the ele‐
20 ments of the list are to be matched against pattern and must have one
21 of the values below:
22
23 MATCHING STYLE OPTIONS
24 If all matching style options are omitted, the default matching style
25 is -glob. If more than one matching style is specified, the last
26 matching style given takes precedence.
27
28 -exact Pattern is a literal string that is compared for exact equality
29 against each list element.
30
31 -glob Pattern is a glob-style pattern which is matched against each
32 list element using the same rules as the string match command.
33
34 -regexp
35 Pattern is treated as a regular expression and matched against
36 each list element using the rules described in the re_syntax
37 reference page.
38
39 -sorted
40 The list elements are in sorted order. If this option is speci‐
41 fied, lsearch will use a more efficient searching algorithm to
42 search list. If no other options are specified, list is assumed
43 to be sorted in increasing order, and to contain ASCII strings.
44 This option is mutually exclusive with -glob and -regexp, and is
45 treated exactly like -exact when either -all or -not are speci‐
46 fied.
47
48 GENERAL MODIFIER OPTIONS
49 These options may be given with all matching styles.
50
51 -all Changes the result to be the list of all matching indices (or
52 all matching values if -inline is specified as well.) If indices
53 are returned, the indices will be in numeric order. If values
54 are returned, the order of the values will be the order of those
55 values within the input list.
56
57 -inline
58 The matching value is returned instead of its index (or an empty
59 string if no value matches.) If -all is also specified, then
60 the result of the command is the list of all values that
61 matched.
62
63 -not This negates the sense of the match, returning the index of the
64 first non-matching value in the list.
65
66 -start index
67 The list is searched starting at position index. The interpre‐
68 tation of the index value is the same as for the command string
69 index, supporting simple index arithmetic and indices relative
70 to the end of the list.
71
72 CONTENTS DESCRIPTION OPTIONS
73 These options describe how to interpret the items in the list being
74 searched. They are only meaningful when used with the -exact and
75 -sorted options. If more than one is specified, the last one takes
76 precedence. The default is -ascii.
77
78 -ascii The list elements are to be examined as Unicode strings (the
79 name is for backward-compatibility reasons.)
80
81 -dictionary
82 The list elements are to be compared using dictionary-style com‐
83 parisons (see lsort for a fuller description). Note that this
84 only makes a meaningful difference from the -ascii option when
85 the -sorted option is given, because values are only dictionary-
86 equal when exactly equal.
87
88 -integer
89 The list elements are to be compared as integers.
90
91 -nocase
92 Causes comparisons to be handled in a case-insensitive manner.
93 Has no effect if combined with the -dictionary, -integer, or
94 -real options.
95
96 -real The list elements are to be compared as floating-point values.
97
98 SORTED LIST OPTIONS
99 These options (only meaningful with the -sorted option) specify how the
100 list is sorted. If more than one is given, the last one takes prece‐
101 dence. The default option is -increasing.
102
103 -decreasing
104 The list elements are sorted in decreasing order. This option
105 is only meaningful when used with -sorted.
106
107 -increasing
108 The list elements are sorted in increasing order. This option
109 is only meaningful when used with -sorted.
110
111 -bisect
112 Inexact search when the list elements are in sorted order. For │
113 an increasing list the last index where the element is less than │
114 or equal to the pattern is returned. For a decreasing list the │
115 last index where the element is greater than or equal to the │
116 pattern is returned. If the pattern is before the first element │
117 or the list is empty, -1 is returned. This option implies │
118 -sorted and cannot be used with either -all or -not.
119
120 NESTED LIST OPTIONS
121 These options are used to search lists of lists. They may be used with
122 any other options.
123
124 -index indexList
125 This option is designed for use when searching within nested
126 lists. The indexList argument gives a path of indices (much as
127 might be used with the lindex or lset commands) within each ele‐
128 ment to allow the location of the term being matched against.
129
130 -subindices
131 If this option is given, the index result from this command (or
132 every index result when -all is also specified) will be a com‐
133 plete path (suitable for use with lindex or lset) within the
134 overall list to the term found. This option has no effect un‐
135 less the -index is also specified, and is just a convenience
136 short-cut.
137
139 Basic searching:
140
141 lsearch {a b c d e} c
142 → 2
143 lsearch -all {a b c a b c} c
144 → 2 5
145
146 Using lsearch to filter lists:
147
148 lsearch -inline {a20 b35 c47} b*
149 → b35
150 lsearch -inline -not {a20 b35 c47} b*
151 → a20
152 lsearch -all -inline -not {a20 b35 c47} b*
153 → a20 c47
154 lsearch -all -not {a20 b35 c47} b*
155 → 0 2
156
157 This can even do a “set-like” removal operation:
158
159 lsearch -all -inline -not -exact {a b c a d e a f g a} a
160 → b c d e f g
161
162 Searching may start part-way through the list:
163
164 lsearch -start 3 {a b c a b c} c
165 → 5
166
167 It is also possible to search inside elements:
168
169 lsearch -index 1 -all -inline {{a abc} {b bcd} {c cde}} *bc*
170 → {a abc} {b bcd}
171
173 foreach(n), list(n), lappend(n), lindex(n), linsert(n), llength(n),
174 lset(n), lsort(n), lrange(n), lreplace(n), string(n)
175
177 binary search, linear search, list, match, pattern, regular expression,
178 search, string
179
180
181
182Tcl 8.6 lsearch(n)