lsearch
Finds the elements of a list that match a pattern.
lsearch ?OPTION ...? LIST PATTERN
Description
Searches LIST for an element that matches PATTERN, and gives the index of the first one, or -1 when no element matches. The options choose how an element matches, what the search gives, and where it starts.
The way of matching is one of -glob (the default), -exact, -regexp, and -sorted. With -exact and -sorted, the type options -ascii (the default), -dictionary, -integer, and -real say how two values compare. -nocase compares letters with no regard to case.
-all gives each match in place of the first, -inline gives the elements in place of their indexes, and -not finds the elements that do not match. -index compares an element of each element, and -stride reads the list as groups. A unique prefix of an option is the option, and the options come before LIST.
An empty LIST gives -1, or an empty list with -all or -inline.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| LIST | list | required | The list to search. |
| PATTERN | glob pattern, value, or regular expression | required | What to find, read by the way of matching. |
Options
| Option | Type | Default | Summary |
|---|---|---|---|
-glob | flag | none | Matches PATTERN as a glob pattern; the default. |
-exact | flag | none | Matches an element that equals PATTERN. |
-regexp | flag | none | Matches PATTERN as a regular expression. |
-sorted | flag | none | Searches a sorted list with a binary search. |
-bisect | flag | none | Gives the index of the last element at or before PATTERN in a sorted list. |
-all | flag | none | Gives each match in place of the first one. |
-inline | flag | none | Gives the matching elements in place of their indexes. |
-not | flag | none | Finds the elements that do not match. |
-start | index | 0 | Starts the search at an index. |
-ascii | flag | none | Compares the elements as text; the default type. |
-dictionary | flag | none | Compares the elements in dictionary order. |
-integer | flag | none | Compares the elements as integers. |
-real | flag | none | Compares the elements as floating-point numbers. |
-nocase | flag | none | Matches letters with no regard to case. |
-increasing | flag | none | Says that a sorted list is in increasing order; the default. |
-decreasing | flag | none | Says that a sorted list is in decreasing order. |
-index | index, or a list of indexes | none | Compares an element of each element in place of the whole element. |
-subindices | flag | none | With -index, gives the path of the match or the subelement. |
-stride | integer, 1 or more | 1 | Reads the list as groups of N elements. Tcl 9.1 only |
-glob
Matches PATTERN as a glob pattern; the default.
Matches each element against PATTERN with the rules of string match: *, ?, and [chars]. It is the way of matching with no option of its kind.
% lsearch -glob {abc} a?c
0
% lsearch -glob -nocase {ABC} a*
0Each text is a valid glob pattern.
-exact
Matches an element that equals PATTERN.
Matches an element that is the same value as PATTERN, with the comparison of the type option: text for -ascii, numbers for -integer and -real. A glob character such as * is a plain character.
% lsearch -exact {a* b} a*
0
% lsearch -exact -integer {01 2} 1
0Each value is a valid PATTERN for an exact match.
-regexp
Matches PATTERN as a regular expression.
Matches an element that has a match of the regular expression PATTERN anywhere in it, with the rules of regexp. Anchor it with ^ and $ for a match of the whole element.
% lsearch -regexp {abc xyz} {^x}
1
% lsearch -regexp -nocase {ABC} {^a}
0% lsearch -regexp {abc} (
couldn't compile regular expression pattern: parentheses () not balanced
-errorcode REGEXP REG_EPAREN {parentheses () not balanced}-sorted
Searches a sorted list with a binary search.
LIST must be sorted in the order of the type option and of -increasing or -decreasing, and the search is a binary search for an element equal to PATTERN. On a list that is not sorted, the result has no meaning. With -all it gives each equal element. With -start it searches from that index.
-sorted is an exact match: PATTERN has no glob characters.
% lsearch -sorted {a b c d} c
2
% lsearch -sorted -integer {1 5 9 12} 9
2
% lsearch -all -sorted {a a b} a
0 1% lsearch -sorted -bisect -all {a b} a
-bisect is not compatible with -all or -not
-errorcode NONE-bisect
Gives the index of the last element at or before PATTERN in a sorted list.
With -sorted, the search gives the index of the last element that is less than or equal to PATTERN in the sort order, or -1 when each element is greater. It does not need an equal element. It cannot go with -all or -not.
% lsearch -sorted -bisect {a b d} c
1
% lsearch -sorted -bisect -integer {1 5 9} 6
1
% lsearch -sorted -bisect {b c} a
-1% lsearch -sorted -bisect -all {a b} a
-bisect is not compatible with -all or -not
-errorcode NONE-all
Gives each match in place of the first one.
Gives a list of the index of each matching element, in order, or an empty list when no element matches. With -inline it gives the elements.
% lsearch -all {a b a} a
0 2
% lsearch -all {a b} z% lsearch -sorted -bisect -all {a b} a
-bisect is not compatible with -all or -not
-errorcode NONE-inline
Gives the matching elements in place of their indexes.
Gives the first matching element, or an empty string when no element matches. With -all it gives a list of each matching element.
% lsearch -inline {apple box} b*
box
% lsearch -inline {apple box} z*A flag has no value to check.
-not
Finds the elements that do not match.
Turns the test around: the search finds the elements that do not match PATTERN. It goes with each way of matching except -bisect.
% lsearch -not {a b c} a
1
% lsearch -not -all {a b c} a
1 2
% lsearch -inline -not {a b} a
b% lsearch -sorted -bisect -not {a b} a
-bisect is not compatible with -all or -not
-errorcode NONE-start INDEX
Starts the search at an index.
The search starts at INDEX and goes to the end, and the indexes of the result are indexes of the whole list. end and end-N count from the last element.
% lsearch -start 2 {a b a b} a
2
% lsearch -all -start 1 {a b a} a
2
% lsearch -start end {a b a} a
2% lsearch -start x {a} a
bad index "x": must be integer?[+-]integer? or end?[+-]integer?
-errorcode NONE-ascii
Compares the elements as text; the default type.
With -exact and -sorted, two values are equal when their text is the same, and -sorted uses the order of the characters.
% lsearch -exact -ascii {10 9} 9
1A flag has no value to check.
-dictionary
Compares the elements in dictionary order.
With -sorted, the order is the order of lsort -dictionary: runs of digits compare as numbers. An exact match is still a match of the same text, so -dictionary does not fold the case of letters.
% lsearch -sorted -dictionary {a2 a10} a10
1
% lsearch -exact -dictionary {A b} a
-1A flag has no value to check.
-integer
Compares the elements as integers.
With -exact and -sorted, two values are equal when they are the same integer, so 01 equals 1. An element that is not an integer does not match.
% lsearch -exact -integer {1 01 2} 1
0
% lsearch -integer {a 1} x
-1An element or a PATTERN that is not an integer does not match, and the search goes on.
-real
Compares the elements as floating-point numbers.
With -exact and -sorted, two values are equal when they are the same number, so 1.0 equals 1.
% lsearch -exact -real {1.0 2} 1
0
% lsearch -sorted -real {1.5 2.5} 2.5
1An element that is not a number does not match.
-nocase
Matches letters with no regard to case.
Compares the letters of PATTERN and of each element as lowercase, for each way of matching.
% lsearch -nocase {A b} a
0
% lsearch -sorted -nocase {A b C} c
2A flag has no value to check.
-increasing
Says that a sorted list is in increasing order; the default.
With -sorted, the list goes from the smallest element to the largest. With no -sorted the option has no effect.
% lsearch -sorted -increasing {a b c} b
1
% lsearch -increasing {b a} a
1A flag has no value to check.
-decreasing
Says that a sorted list is in decreasing order.
With -sorted, the list goes from the largest element to the smallest. With no -sorted the option has no effect.
% lsearch -sorted -decreasing -integer {9 5 1} 5
1
% lsearch -decreasing {b a} a
1A flag has no value to check.
-index INDEX
Compares an element of each element in place of the whole element.
Each element of LIST is a list, and the search compares its element at INDEX, or at the path of a list of indexes. Each element must have that index. The result is the index of the element of LIST, or the whole element with -inline.
% lsearch -index 1 {{x 1} {y 2}} 2
1
% lsearch -index 1 -inline {{x 1} {y 2}} 2
y 2
% lsearch -index 0 -sorted {{a 1} {b 2}} b
1% lsearch -index 5 {{x 1}} 2
element 5 missing from sublist "x 1"
-errorcode NONE-subindices
With -index, gives the path of the match or the subelement.
With -index, the result of a match is the index of the element followed by the indexes of -index: a path for lindex. With -inline it is the subelement at that path in place of the whole element, except in Tcl 8.6 with no -all: there it is the whole element.
% lsearch -subindices -index 1 {{x 1} {y 2}} 2
1 1
# Tcl 9.1 only
% lsearch -subindices -index 1 -inline {{x 1} {y 2}} 2
2
# Tcl 8.6 only
% lsearch -subindices -index 1 -inline {{x 1} {y 2}} 2
y 2
% lsearch -subindices -index 1 -all -inline {{x 1} {y 2}} *
1 2% lsearch -subindices -index 5 {{x 1}} 2
element 5 missing from sublist "x 1"
-errorcode NONE-stride N Tcl 9.1 only
Reads the list as groups of N elements.
The list is groups of N elements, and its length must be a multiple of N. The search compares the first element of each group, or the element of -index in the group, and gives the index of the first element of the group. With -inline it gives the whole group.
% lsearch -stride 2 {a 1 b 2} b
2
% lsearch -stride 2 -index 1 {a 1 b 2} 2
2
% lsearch -stride 2 -inline {a 1 b 2} b
b 2
% lsearch -stride 2 -all {a 1 a 2} a
0 2% lsearch -stride 3 {a b} a
list size must be a multiple of the stride length
-errorcode NONE
% lsearch -stride 0 {a} a
stride length must be at least 1
-errorcode NONEReturns
The index of the first match or -1, the indexes of each match with -all, or the elements with -inline.
Examples
% lsearch {a b c} c
2
% lsearch {a b c} z
-1
% lsearch {apple box ant} a*
0
% lsearch -all -inline {apple box ant} a*
apple ant
% lsearch -index 0 {{x 1} {y 2}} y
1
% lsearch -exact -all -inline -not {a b c b} b
a c
Errors
% lsearch {a}
wrong # args: should be "lsearch ?-option value ...? list pattern"
-errorcode TCL WRONGARGS
# Tcl 9.1 only
% lsearch -bogus {a} a
bad option "-bogus": must be -all, -ascii, -bisect, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, -stride, or -subindices
-errorcode NONE
# Tcl 8.6 only
% lsearch -bogus {a} a
bad option "-bogus": must be -all, -ascii, -bisect, -decreasing, -dictionary, -exact, -glob, -increasing, -index, -inline, -integer, -nocase, -not, -real, -regexp, -sorted, -start, or -subindices
-errorcode NONE
% lsearch "a {b" a
unmatched open brace in list
-errorcode TCL VALUE LIST BRACE
Tcl 8.6 and Tcl 9.1
- Tcl 9.1 has the option
-stride. - With
-subindices -inlineand no-all, Tcl 8.6 gives the whole element and Tcl 9.1 gives the subelement. With-allboth give the subelements.
Differences from Tcl
- Most errors have
-errorcode NONE, where Tcl 8.6.16 and 9.0.4 give a code such asTCL LOOKUP INDEX optionorTCL VALUE INDEX(issue 658). - In Tcl 9.0.4 the message of a bad regular expression starts with
cannot compile(issue 668).
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1