Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/lsearch.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 6.7 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\" Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>.  All rights reserved.
5'\" Copyright (c) 2003-2004 Donal K. Fellows.
6'\"
7'\" See the file "license.terms" for information on usage and redistribution
8'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9'\"
10'\" RCS: @(#) $Id: lsearch.n,v 1.34 2008/03/26 09:59:22 dkf Exp $
11'\"
12.so man.macros
13.TH lsearch n 8.5 Tcl "Tcl Built-In Commands"
14.BS
15'\" Note:  do not modify the .SH NAME line immediately below!
16.SH NAME
17lsearch \- See if a list contains a particular element
18.SH SYNOPSIS
19\fBlsearch \fR?\fIoptions\fR? \fIlist pattern\fR
20.BE
21
22.SH DESCRIPTION
23.PP
24This command searches the elements of \fIlist\fR to see if one
25of them matches \fIpattern\fR.  If so, the command returns the index
26of the first matching element
27(unless the options \fB\-all\fR or \fB\-inline\fR are specified.)
28If not, the command returns \fB\-1\fR.  The \fIoption\fR arguments
29indicates how the elements of the list are to be matched against
30\fIpattern\fR and must have one of the values below:
31.SS "MATCHING STYLE OPTIONS"
32If all matching style options are omitted, the default matching style
33is \fB\-glob\fR.  If more than one matching style is specified, the
34last matching style given takes precedence.
35.TP
36\fB\-exact\fR
37\fIPattern\fR is a literal string that is compared for exact equality
38against each list element.
39.TP
40\fB\-glob\fR
41\fIPattern\fR is a glob-style pattern which is matched against each list
42element using the same rules as the \fBstring match\fR command.
43.TP
44\fB\-regexp\fR
45\fIPattern\fR is treated as a regular expression and matched against
46each list element using the rules described in the \fBre_syntax\fR
47reference page.
48.TP
49\fB\-sorted\fR
50The list elements are in sorted order.  If this option is specified,
51\fBlsearch\fR will use a more efficient searching algorithm to search
52\fIlist\fR.  If no other options are specified, \fIlist\fR is assumed
53to be sorted in increasing order, and to contain ASCII strings.  This
54option is mutually exclusive with \fB\-glob\fR and \fB\-regexp\fR, and
55is treated exactly like \fB\-exact\fR when either \fB\-all\fR or
56\fB\-not\fR are specified.
57.SS "GENERAL MODIFIER OPTIONS"
58These options may be given with all matching styles.
59.TP
60\fB\-all\fR
61.
62Changes the result to be the list of all matching indices (or all matching
63values if \fB\-inline\fR is specified as well.) If indices are returned, the
64indices will be in numeric order. If values are returned, the order of the
65values will be the order of those values within the input \fIlist\fR.
66.TP
67\fB\-inline\fR
68The matching value is returned instead of its index (or an empty
69string if no value matches.)  If \fB\-all\fR is also specified, then
70the result of the command is the list of all values that matched.
71.TP
72\fB\-not\fR
73This negates the sense of the match, returning the index of the first
74non-matching value in the list.
75.TP
76\fB\-start\fR\0\fIindex\fR
77The list is searched starting at position \fIindex\fR.
78.VS 8.5
79The interpretation of the \fIindex\fR value is the same as
80for the command \fBstring index\fR, supporting simple index
81arithmetic and indices relative to the end of the list.
82.VE 8.5
83.SS "CONTENTS DESCRIPTION OPTIONS"
84These options describe how to interpret the items in the list being
85searched.  They are only meaningful when used with the \fB\-exact\fR
86and \fB\-sorted\fR options.  If more than one is specified, the last
87one takes precedence.  The default is \fB\-ascii\fR.
88.TP
89\fB\-ascii\fR
90The list elements are to be examined as Unicode strings (the name is
91for backward-compatibility reasons.)
92.TP
93\fB\-dictionary\fR
94The list elements are to be compared using dictionary-style
95comparisons (see \fBlsort\fR for a fuller description). Note that this
96only makes a meaningful difference from the \fB\-ascii\fR option when
97the \fB\-sorted\fR option is given, because values are only
98dictionary-equal when exactly equal.
99.TP
100\fB\-integer\fR
101The list elements are to be compared as integers.
102.VS 8.5
103.TP
104\fB\-nocase\fR
105Causes comparisons to be handled in a case-insensitive manner.  Has no
106effect if combined with the \fB\-dictionary\fR, \fB\-integer\fR, or
107\fB\-real\fR options.
108.VE 8.5
109.TP
110\fB\-real\fR
111The list elements are to be compared as floating-point values.
112.SS "SORTED LIST OPTIONS"
113These options (only meaningful with the \fB\-sorted\fR option) specify
114how the list is sorted.  If more than one is given, the last one takes
115precedence.  The default option is \fB\-increasing\fR.
116.TP
117\fB\-decreasing\fR
118The list elements are sorted in decreasing order.  This option is only
119meaningful when used with \fB\-sorted\fR.
120.TP
121\fB\-increasing\fR
122The list elements are sorted in increasing order.  This option is only
123meaningful when used with \fB\-sorted\fR.
124.SS "NESTED LIST OPTIONS"
125.VS 8.5
126These options are used to search lists of lists.  They may be used
127with any other options.
128.TP
129\fB\-index\fR\0\fIindexList\fR
130This option is designed for use when searching within nested lists.
131The \fIindexList\fR argument gives a path of indices (much as might be
132used with the \fBlindex\fR or \fBlset\fR commands) within each element
133to allow the location of the term being matched against.
134.TP
135\fB\-subindices\fR
136If this option is given, the index result from this command (or every
137index result when \fB\-all\fR is also specified) will be a complete
138path (suitable for use with \fBlindex\fR or \fBlset\fR) within the
139overall list to the term found.  This option has no effect unless the
140\fI\-index\fR is also specified, and is just a convenience short-cut.
141.VE 8.5
142.SH EXAMPLES
143Basic searching:
144.CS
145\fBlsearch\fR {a b c d e} c
146      \fI\(-> 2\fR
147\fBlsearch\fR -all {a b c a b c} c
148      \fI\(-> 2 5\fR
149.CE
150.PP
151Using \fBlsearch\fR to filter lists:
152.CS
153\fBlsearch\fR -inline {a20 b35 c47} b*
154      \fI\(-> b35\fR
155\fBlsearch\fR -inline -not {a20 b35 c47} b*
156      \fI\(-> a20\fR
157\fBlsearch\fR -all -inline -not {a20 b35 c47} b*
158      \fI\(-> a20 c47\fR
159\fBlsearch\fR -all -not {a20 b35 c47} b*
160      \fI\(-> 0 2\fR
161.CE
162.PP
163This can even do a
164.QW set-like
165removal operation:
166.CS
167\fBlsearch\fR -all -inline -not -exact {a b c a d e a f g a} a
168      \fI\(-> b c d e f g\fR
169.CE
170.PP
171Searching may start part-way through the list:
172.CS
173\fBlsearch\fR -start 3 {a b c a b c} c
174      \fI\(-> 5\fR
175.CE
176.PP
177It is also possible to search inside elements:
178.CS
179\fBlsearch\fR -index 1 -all -inline {{a abc} {b bcd} {c cde}} *bc*
180      \fI\(-> {a abc} {b bcd}\fR
181.CE
182.SH "SEE ALSO"
183foreach(n), list(n), lappend(n), lindex(n), linsert(n), llength(n),
184lset(n), lsort(n), lrange(n), lreplace(n),
185.VS 8.5
186string(n)
187.VE
188.SH KEYWORDS
189list, match, pattern, regular expression, search, string
190'\" Local Variables:
191'\" mode: nroff
192'\" End:
Note: See TracBrowser for help on using the repository browser.