lsearch(n)
π lsearch(n) β Tcl Built-In Commands π
okay bestie, gather round, because lsearch is about to become your new fave. it's a Tcl built-in command, she's giving "does this list contain my thing" energy, and she does it flawlessly. let's go π¦
β¨ SYNOPSIS β¨
lsearch ?options? list pattern
πΈ DESCRIPTION πΈ
so basically, this command goes through the elements of list and checks if any of them match pattern. if it finds one, it returns the index of the first matching element β unless you passed -all or -inline, in which case it does its own cute thing instead. if nothing matches? it returns -1, or (if -all or -inline were specified) it returns the empty string. ngl that distinction matters, don't sleep on it.
the options argument is how you tell it how to match elements against pattern, and it's gotta be one of the values below. no cap. π
π MATCHING STYLE OPTIONS π
if you skip all the matching style options, the default matching style is -glob. and if you specify more than one matching style, the last matching style you gave wins. periodt.
-exactβpatternis a literal string, compared for exact equality against each list element. simple, clean, iconic.-globβpatternis a glob-style pattern, matched against each list element using the same rules as thestring matchcommand.-regexpβpatternis treated as a regular expression and matched against each list element using the rules in there_syntaxreference page. spicy but valid.-sortedβ the list elements are in sorted order. if you say this,lsearchgets to use a way more efficient searching algorithm onlist. if no other options are specified,listis assumed to be sorted in increasing order and to contain ASCII strings. this option is mutually exclusive with-globand-regexp, and it's treated exactly like-exactwhen either-allor-notis specified. π
π GENERAL MODIFIER OPTIONS π
these options play nice with all matching styles. love that for them.
-allβ changes the result to be the list of all matching indices (or all matching values if-inlineis specified as well). if indices are returned, they come back in ascending numeric order. if values are returned, the order of the values follows the order of those values within the input list.-inlineβ returns the matching value instead of its index (or an empty string if no value matches). and if-allis also specified, then the result is the list of all the values that matched. π«Ά-notβ negates the sense of the match, returning the index of the first non-matching value in the list. chaotic but useful.-startindex β the list is searched starting at position index. the interpretation of the index value is the same as for the commandstring index, supporting simple index arithmetic and indices relative to the end of the list.
π CONTENTS DESCRIPTION OPTIONS π
these describe how to interpret the items in the list you're searching. heads up: they're only meaningful when used with the -exact and -sorted options. if more than one is specified, the last one takes precedence. the default is -ascii.
-asciiβ the list elements are examined as Unicode strings (the name is like that for backward-compatibility reasons, don't ask π).-dictionaryβ the list elements are compared using dictionary-style comparisons (peeplsortfor a fuller description). note that this only makes a meaningful difference from the-asciioption when the-sortedoption is given, because values are only dictionary-equal when they're exactly equal.-integerβ the list elements are compared as integers.-nocaseβ comparisons are handled in a case-insensitive manner. has no effect if combined with the-dictionary,-integer, or-realoptions.-realβ the list elements are compared as floating-point values.
π§ SORTED LIST OPTIONS π§
these are only meaningful with the -sorted option, and they specify how the list is sorted. if more than one is given, the last one takes precedence. the default option is -increasing.
-decreasingβ the list elements are sorted in decreasing order. only meaningful when used with-sorted.-increasingβ the list elements are sorted in increasing order. only meaningful when used with-sorted.-bisectβ inexact search when the list elements are in sorted order. for an increasing list, the last index where the element is less than or equal to the pattern is returned. for a decreasing list, the last index where the element is greater than or equal to the pattern is returned. if the pattern is before the first element, or the list is empty,-1is returned. this option implies-sortedand cannot be used with either-allor-not. π
π NESTED LIST OPTIONS π
these are for searching lists of lists. they may be used with any other options. yes, any. go off.
-stridestrideLength β if specified, the list is treated as consisting of groups of strideLength elements, and the groups are searched by either their first element or, if the-indexoption is used, by the element within each group given by the first index passed to-index(which is then ignored by-index). the resulting index always points to the first element in a group.
the list length must be an integer multiple of strideLength, which in turn must be at least 1. a strideLength of 1 is the default and indicates no grouping.
-indexindexList β designed for use when searching within nested lists. the indexList argument gives a path of indices (much as might be used with thelindexorlsetcommands) within each element, so you can locate the term being matched against.-subindicesβ if given, the index result from this command (or every index result when-allis also specified) will be a complete path (suitable for use withlindexorlset) within the overall list to the term found. this option has no effect unless-indexis also specified, and it's just a convenience shortcut. very slay of it.
β EXAMPLES β
basic searching, the classic:
lsearch {a b c d e} c
β 2
lsearch -all {a b c a b c} c
β 2 5
using lsearch to filter lists (this is where she eats):
lsearch -inline {a20 b35 c47} b*
β b35
lsearch -inline -not {a20 b35 c47} b*
β a20
lsearch -all -inline -not {a20 b35 c47} b*
β a20 c47
lsearch -all -not {a20 b35 c47} b*
β 0 2
this can even do a "set-like" removal operation:
lsearch -all -inline -not -exact {a b c a d e a f g a} a
β b c d e f g
searching may start part-way through the list:
lsearch -start 3 {a b c a b c} c
β 5
it is also possible to search inside elements:
lsearch -index 1 -all -inline {{abc abc} {abc bcd} {abc cde}} *bc*
β {abc abc} {abc bcd}
the same thing for a flattened list:
lsearch -stride 2 -index 1 -all -inline {abc abc abc bcd abc cde} *bc*
β abc abc abc bcd
π SEE ALSO π
foreach(n), list(n), lappend(n), lassign(n), ledit(n), lindex(n), linsert(n), llength(n), lmap(n), lpop(n), lrange(n), lremove(n), lrepeat(n), lreplace(n), lreverse(n), lseq(n), lset(n), lsort(n), string(n)
π KEYWORDS π
binary search, linear search, list, match, pattern, regular expression, search, string
and that's the whole page bestie, you're officially a lsearch girlie now. this one's from Tcl 8.6 β go forth and search responsibly πβ¨π¦
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- Copyright (c) 2003-2004 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms