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
14
16 This command searches the elements of list to see if one of them
17 matches pattern. If so, the command returns the index of the first
18 matching element (unless the options -all or -inline are specified.)
19 If not, the command returns -1. The option arguments indicates how the
20 elements of the list are to be matched against pattern and must have
21 one 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 NESTED LIST OPTIONS
112 These options are used to search lists of lists. They may be used with │
113 any other options. │
114
115 -index indexList │
116 This option is designed for use when searching within nested │
117 lists. The indexList argument gives a path of indices (much as │
118 might be used with the lindex or lset commands) within each ele‐ │
119 ment to allow the location of the term being matched against. │
120
121 -subindices │
122 If this option is given, the index result from this command (or │
123 every index result when -all is also specified) will be a com‐ │
124 plete path (suitable for use with lindex or lset) within the │
125 overall list to the term found. This option has no effect │
126 unless the -index is also specified, and is just a convenience │
127 short-cut.
128
130 Basic searching:
131 lsearch {a b c d e} c
132 → 2
133 lsearch -all {a b c a b c} c
134 → 2 5
135
136 Using lsearch to filter lists:
137 lsearch -inline {a20 b35 c47} b*
138 → b35
139 lsearch -inline -not {a20 b35 c47} b*
140 → a20
141 lsearch -all -inline -not {a20 b35 c47} b*
142 → a20 c47
143 lsearch -all -not {a20 b35 c47} b*
144 → 0 2
145
146 This can even do a “set-like” removal operation:
147 lsearch -all -inline -not -exact {a b c a d e a f g a} a
148 → b c d e f g
149
150 Searching may start part-way through the list:
151 lsearch -start 3 {a b c a b c} c
152 → 5
153
154 It is also possible to search inside elements:
155 lsearch -index 1 -all -inline {{a abc} {b bcd} {c cde}} *bc*
156 → {a abc} {b bcd}
157
159 foreach(n), list(n), lappend(n), lindex(n), linsert(n), llength(n),
160 lset(n), lsort(n), lrange(n), lreplace(n), string(n) │
161
163 list, match, pattern, regular expression, search, string
164
165
166
167Tcl 8.5 lsearch(n)