Determining if a key exists

Now that we have a named dictionary with key/value pairs, it becomes necessary to determine if the given key exists. To accomplish this, Tcl provides the dict exists command. The syntax is as follows:

	dict exists DictionaryValue key… key…

How to do it…

In the following example, we will create a dictionary containing a set of key/value pairs and then determine whether or not 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

% dict exists $names 3
1

How it works…

The dict exists command returns a Boolean value to indicate if the specified key exists in the dictionary referenced in name. A return value of 1 indicates that the key exists, while a return of 0 indicates that it does not. Be aware that this command will return an error if dictionaryValue does not reference an existing dictionary.

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

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