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 β€” pattern is a literal string, compared for exact equality against each list element. simple, clean, iconic.
  • -glob β€” pattern is a glob-style pattern, matched against each list element using the same rules as the string match command.
  • -regexp β€” pattern is treated as a regular expression and matched against each list element using the rules in the re_syntax reference page. spicy but valid.
  • -sorted β€” the list elements are in sorted order. if you say this, lsearch gets to use a way more efficient searching algorithm on list. if no other options are specified, list is assumed to be sorted in increasing order and to contain ASCII strings. this option is mutually exclusive with -glob and -regexp, and it's treated exactly like -exact when either -all or -not is 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 -inline is 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 -all is 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.
  • -start index β€” the list is searched starting at position index. the interpretation of the index value is the same as for the command string 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 (peep lsort for a fuller description). note that this only makes a meaningful difference from the -ascii option when the -sorted option 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 -real options.
  • -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, -1 is returned. this option implies -sorted and cannot be used with either -all or -not. πŸ’€

πŸ“ NESTED LIST OPTIONS πŸ“

these are for searching lists of lists. they may be used with any other options. yes, any. go off.

  • -stride strideLength β€” 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 -index option 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.

  • -index indexList β€” designed for use when searching within nested lists. the indexList argument gives a path of indices (much as might be used with the lindex or lset commands) 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 -all is also specified) will be a complete path (suitable for use with lindex or lset) within the overall list to the term found. this option has no effect unless -index is 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