lfilterTcl 9.1 only
Gives the elements of a list for which a script gives true.
lfilter VARLIST LIST ?VARLIST LIST ...? SCRIPT
Description
Runs SCRIPT once for each element of LIST, with the variables of VARLIST set as foreach sets them, and gives a list of the values of the first variable of the first VARLIST for each pass where SCRIPT gives true. The result of SCRIPT must be a boolean.
With more than one name in VARLIST, each pass gets that many elements. With more than one VARLIST and LIST, the lists go in step, as in foreach. continue in SCRIPT leaves the element out, and break ends the loop with the elements kept so far. An empty LIST gives an empty list.
Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
| VARLIST | list of variable names | required | The variables of one list, at least one name. |
| LIST | list | required | A list to go through. |
| SCRIPT | script | required | The test, run for each pass. |
Returns
The list of the kept values of the first variable.
Examples
% lfilter x {1 2 3 4} {expr {$x % 2 == 0}}
2 4
% lfilter {a b} {1 2 3 4} {expr {$b > 2}}
3
% lfilter x {1 2} y {3 4} {expr {$y > 3}}
2
% lfilter x {1 2 3} {if {$x == 2} continue; expr 1}
1 3
% lfilter x {} {expr 1}
Errors
% lfilter x {1 2} {expr {"x"}}
expected boolean value but got "x"
-errorcode NONE
% lfilter {} {1} {expr 1}
lfilter varlist is empty
-errorcode NONE
% lfilter x {1}
wrong # args: should be "lfilter varList list ?varList list ...? command"
-errorcode TCL WRONGARGS
Differences from Tcl
- Tcl 9.0.4 has no
lfilter.
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 only.
- Since
- ztcl 1.0.0-beta.1