dict filter
Gives the keys and values of a dict that pass a test.
dict filter DICT key ?PATTERN ...?
dict filter DICT value ?PATTERN ...?
dict filter DICT script {KEYVAR VALUEVAR} BODY
Description
Gives a new dict with the keys and values of DICT that pass the test of the filter type, in their order.
key: the key matches at least one PATTERN, with the rules ofstring match. With no PATTERN the result is an empty dict.value: the value matches at least one PATTERN.script: BODY runs for each key and value, with KEYVAR and VALUEVAR set, and its result must be a boolean. A true result keeps the key.breakends the loop and gives the keys kept so far, andcontinueleaves the key out.
A unique prefix of a filter type is the type.
Arguments
| Argument | Type | Default | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| DICT | dict | required | The dict. | ||||||||
| key|value|script | filter type | required | The kind of test.
| ||||||||
| PATTERN | glob pattern | none | A pattern of the type key or value. | ||||||||
| BODY | script | required | The test of the type script, with its two variables. |
Returns
The new dict.
Examples
% dict filter {a 1 b 2 ab 3} key a*
a 1 ab 3
% dict filter {a 1 b 2 ab 3} key a* b
a 1 b 2 ab 3
% dict filter {a 1 b 2 c 3} value {[13]}
a 1 c 3
% dict filter {a 1 b 2 c 3} script {k v} {expr {$v > 1}}
b 2 c 3
% dict filter {a 1 b 2 c 3} script {k v} {if {$v == 2} break; expr 1}
a 1
Errors
% dict filter {a 1} bogus x
bad filterType "bogus": must be key, script, or value
-errorcode NONE
% dict filter {a 1} script {k v} {expr {"x"}}
expected boolean value but got "x"
-errorcode NONE
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.1