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