Tcl8.6.10/Tk8.6.10 Documentation > Tcl Commands > lsearch
Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API
- NAME
- lsearch — See if a list contains a particular element
- SYNOPSIS
- DESCRIPTION
- MATCHING STYLE OPTIONS
- GENERAL MODIFIER OPTIONS
- CONTENTS DESCRIPTION OPTIONS
- SORTED LIST OPTIONS
- NESTED LIST OPTIONS
- EXAMPLES
- SEE ALSO
- KEYWORDS
NAME
lsearch — See if a list contains a particular elementSYNOPSIS
lsearch ?options? list patternDESCRIPTION
This command searches the elements of list to see if one of them matches pattern. If so, the command returns the index of the first matching element (unless the options -all or -inline are specified.) If not, the command returns -1. The option arguments indicates how the elements of the list are to be matched against pattern and must have one of the values below:MATCHING STYLE OPTIONS
If all matching style options are omitted, the default matching style is -glob. If more than one matching style is specified, the last matching style given takes precedence.
GENERAL MODIFIER OPTIONS
These options may be given with all matching styles.
CONTENTS DESCRIPTION OPTIONS
These options describe how to interpret the items in the list being searched. They are 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.
SORTED LIST OPTIONS
These options (only meaningful with the -sorted option) specify how the list is sorted. If more than one is given, the last one takes precedence. The default option is -increasing.
NESTED LIST OPTIONS
These options are used to search lists of lists. They may be used with any other options.
EXAMPLES
Basic searching:
lsearch {a b c d e} c → 2 lsearch -all {a b c a b c} c → 2 5
Using lsearch to filter lists:
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 {{a abc} {b bcd} {c cde}} *bc* → {a abc} {b bcd}
SEE ALSO
foreach, list, lappend, lindex, linsert, llength, lset, lsort, lrange, lreplace, stringKEYWORDS
binary search, linear search, list, match, pattern, regular expression, search, stringCopyright © 1993 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.
Copyright © 2001 Kevin B. Kenny <kennykb(at)acm.org>. All rights reserved.
Copyright © 2003-2004 Donal K. Fellows.
Copyright © 1994-1996 Sun Microsystems, Inc.
Copyright © 2001 Kevin B. Kenny <kennykb(at)acm.org>. All rights reserved.
Copyright © 2003-2004 Donal K. Fellows.