Update docs
This commit is contained in:
+21
-11
@@ -13,21 +13,21 @@ lfilter \- Select elements from a list based on an expression
|
||||
.SH SYNOPSIS
|
||||
\fBlfilter \fIvarname list expression\fR
|
||||
.br
|
||||
\fBlfilter \fIvarlist1 list1\fR ?\fIvarlist2 list2 ...\fR? \fIexpression\fR
|
||||
\fBlfilter \fIvarlist1 list1\fR ?\fIvarlist2 list2 ...\fR? \fIbody\fR
|
||||
.BE
|
||||
.SH DESCRIPTION
|
||||
.PP
|
||||
The \fBlfilter\fR command implements a loop where the loop variable(s) take on
|
||||
values from one or more lists, and the loop returns a list of values from the
|
||||
(first) list where the \fIexpression\fR evaluates to a true value.
|
||||
(first) list where the \fIbody\fR script evaluates to a true value.
|
||||
.PP
|
||||
In the simplest case there is one loop variable, \fIvarname\fR, and one list,
|
||||
\fIlist\fR, that is a list of values to assign to \fIvarname\fR. The
|
||||
\fIexpression\fR argument is a Tcl expression. For each element of \fIlist\fR
|
||||
\fIbody\fR argument is a Tcl script. For each element of \fIlist\fR
|
||||
(in order from first to last), \fBlfilter\fR assigns the contents of the
|
||||
element to \fIvarname\fR as if the \fBlindex\fR command had been used to
|
||||
extract the element, then calls the Tcl interpreter to execute \fIexpression\fR.
|
||||
If execution of the expression completes normally and produces a true value,
|
||||
extract the element, then calls the Tcl interpreter to execute \fIbody\fR.
|
||||
If execution of the script completes normally and produces a true value,
|
||||
that element of \fIlist\fR is appended to an accumulator list. \fBlmap\fR
|
||||
returns the accumulator list.
|
||||
.PP
|
||||
@@ -40,7 +40,7 @@ from first to last, and each value is used exactly once. The total number of
|
||||
loop iterations is large enough to use up all the values from all the value
|
||||
lists. If a value list does not contain enough elements for each of its loop
|
||||
variables in each iteration, empty values are used for the missing elements.
|
||||
For each iteration, the value that is appended when \fIexpression\fR yields
|
||||
For each iteration, the value that is appended when \fIbody\fR yields
|
||||
a true value (i.e., anything accepted by \fBTcl_GetBooleanFromObj\fR as a
|
||||
boolean) is the value that was assigned to the first variable of the first
|
||||
list of variables; all other values are not collected.
|
||||
@@ -55,7 +55,7 @@ Select the elements of the list that are odd:
|
||||
.PP
|
||||
.CS
|
||||
\fBlfilter\fR x {0 2 4 1 3 5 10 8 6 11 9 7} {
|
||||
($x % 2) == 1
|
||||
expr {($x % 2) == 1}
|
||||
}
|
||||
# The result is "1 3 5 11 9 7"
|
||||
.CE
|
||||
@@ -64,7 +64,7 @@ Select elements of a list according to a condition in another list:
|
||||
.PP
|
||||
.CS
|
||||
\fBlfilter\fR x [lseq 10] y [lseq 0 .. 1 by 0.1] {
|
||||
$y > 0.3 && $y < 0.7
|
||||
expr {$y > 0.3 && $y < 0.7}
|
||||
}
|
||||
# The result is "4 5 6"
|
||||
.CE
|
||||
@@ -74,14 +74,24 @@ Select the elements containing at least five characters:
|
||||
.CS
|
||||
set sentence "The quick brown fox jumps over the lazy dog."
|
||||
\fBlfilter\fR word [split $sentence] {
|
||||
[string length $word] >= 5
|
||||
expr {[string length $word] >= 5}
|
||||
}
|
||||
# The result is "quick brown jumps"
|
||||
.CE
|
||||
.PP
|
||||
Select the elements that are true palindromes:
|
||||
.PP
|
||||
.CS
|
||||
set usernames {tom bob steve dood}
|
||||
\fBlfilter\fR word $usernames {
|
||||
string equal $word [string reverse $word]
|
||||
}
|
||||
# The result is "bob dood"
|
||||
.CE
|
||||
.SH "SEE ALSO"
|
||||
foreach(n), lmap(n), lsearch(n)
|
||||
dict(n), foreach(n), lmap(n), lsearch(n)
|
||||
.SH KEYWORDS
|
||||
foreach, iteration, list, loop, expression
|
||||
foreach, iteration, list, loop, filter
|
||||
'\" Local Variables:
|
||||
'\" mode: nroff
|
||||
'\" End:
|
||||
|
||||
Reference in New Issue
Block a user