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.

A unique prefix of a filter type is the type.

Arguments

ArgumentTypeDefaultDescription
DICTdictrequiredThe dict.
key|value|scriptfilter typerequiredThe kind of test.
ValueMeaning
keyTests each key against the patterns.
valueTests each value against the patterns.
scriptRuns BODY for each key and value.
PATTERNglob patternnoneA pattern of the type key or value.
BODYscriptrequiredThe 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

See also