Practical Programming in Tcl & Tk, Third Edition By Brent B. Welch
Table of Contents
Chapter 5. Tcl Lists
Searching Lists: lsearch
lsearch returns the index of a value in the list, or -1 if it is not present. lsearch supports pattern matching in its search. Glob-style pattern matching is the default, and this can be disabled with the -exact flag. The semantics of glob pattern matching is described in Chapter 4. The -regexp option lets you specify the list value with a regular expression. Regular expressions are described in Chapter 11. In the following example, the glob pattern l* matches the value list.
lsearch {here is a list}l* => 3
Example 5-6 uses lreplace and lsearch to delete a list element by value. The value is found with lsearch. The value is removed with an lreplace that does not specify any replacement list elements:
Example 5-6 Deleting a list element by value.
proc ldelete {list value } { set ix [lsearch -exact $list $value] if {$ix >= 0} { return [lreplace $list $ix $ix] } else { return $list } }