Filtering a dictionary in Tcl allows us to create a new dictionary containing the filtered key/value pairs, as opposed to simply returning a filtered listing. This allows us to isolate the data desired and interact with it dynamically. To accomplish this, Tcl provides the dict filter
command. The syntax is as follows:
dict filter dictionaryValue filter_type argument1 argument2 …
Various filter types are supported by the command. The filters are as follows:
Option |
Interpretation |
---|---|
Key |
The key rule matches the key/value pair whose keys match the defined pattern, as in a string match. |
Value |
The value rule matches the key/value pairs whose value matches the defined pattern, as in a string match. |
Script |
The script rule tests for matching by assigning the key to a key variable and the value to a value variable, and then evaluating the given script, which must return a Boolean value. Only those sets that return the value |