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

ArgumentTypeDefaultDescription
LISTlistrequiredThe list to search.
PATTERNglob pattern, value, or regular expressionrequiredWhat to find, read by the way of matching.

Options

OptionTypeDefaultSummary
-globflagnoneMatches PATTERN as a glob pattern; the default.
-exactflagnoneMatches an element that equals PATTERN.
-regexpflagnoneMatches PATTERN as a regular expression.
-sortedflagnoneSearches a sorted list with a binary search.
-bisectflagnoneGives the index of the last element at or before PATTERN in a sorted list.
-allflagnoneGives each match in place of the first one.
-inlineflagnoneGives the matching elements in place of their indexes.
-notflagnoneFinds the elements that do not match.
-startindex0Starts the search at an index.
-asciiflagnoneCompares the elements as text; the default type.
-dictionaryflagnoneCompares the elements in dictionary order.
-integerflagnoneCompares the elements as integers.
-realflagnoneCompares the elements as floating-point numbers.
-nocaseflagnoneMatches letters with no regard to case.
-increasingflagnoneSays that a sorted list is in increasing order; the default.
-decreasingflagnoneSays that a sorted list is in decreasing order.
-indexindex, or a list of indexesnoneCompares an element of each element in place of the whole element.
-subindicesflagnoneWith -index, gives the path of the match or the subelement.
-strideinteger, 1 or more1Reads 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*
0

Each 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
0

Each 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
1

A 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
-1

A 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
-1

An 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
1

An 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
2

A 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
1

A 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
1

A 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 NONE

Returns

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

Differences from Tcl

Facts

Capability
None.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also