Filtering a dictionary

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 true are included within the new dictionary.

If the script returns a TCL_BREAK, no other comparisons are performed.

In the event of a TCL_CONTINUE return, the return is treated as a TCL_OK.

How to do it…

In the following example we will create a dictionary containing a set of key/value pairs and then filter to determine if a specific key exists. Return values from the commands are provided for clarity. Enter the following command:


% set names [dict create 1 John 2 Mary 3 Paul]
1 John 2 Mary 3 Paul

% set filtered [dict filter $names key 1]
1 John

How it works…

The dict filter command accepts a named dictionary as referenced by dictionaryValue and returns a new dictionary containing the key/value pairs that match the filtering criteria as defined in the argument or arguments provided.

There's more…

In the following example, we will create a dictionary containing a set of key/value pairs and then filter to determine if a specific value exists. Return values from the commands are provided for clarity. Enter the following command:


% set names [dict create 1 John 2 Joe 3 Paul]
1 John 2 Joe 3 Paul
% set filtered [dict filter $names value Jo*]
1 John 2 Joe

As you can see, the dict filter command accepted the arguments and based on the existence of the value to be filtered, it has returned a new dictionary named filtered, containing the located key/value pairs.

In the following example, we will create a dictionary containing a set of key/value pairs and then filter to determine if a specific value exists, by using the script keyword. Return values from the commands are provided for clarity. Enter the following command:


% set filtered [dict filter $name script {key value} {
	Expr {$key < 2}
}]
1 John

In this instance, the filter command has evaluated each key/value pair and returned those that evaluate as true, in the provided script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.42.116