Determining the size of a dictionary
To more effectively interact with a dictionary, it is beneficial to know how many entries are contained within. To accomplish this, Tcl provides the dict size
command. The syntax is as follows:
dict size dictionaryValue
How to do it…
In the following example, we will create a dictionary containing collections of key/value pairs and then we will use the dict size
command to determine the number of key/value pairs. Return values from the commands are provided for clarity. Enter the following command:
% set test [dict create 1 John 2 Mary 3 Paul] 1 John 2 Mary 3 Paul % dict size $test 3
How it works…
The dict size
command returns a count of the key/value pairs contained in the dictionary.