Getting a record

While the dict for command is generally used for processing multiple key/value pairs, Tcl provides the dict get command to obtain the value assigned to a specific key. The syntax is as follows:

	dict get dictionaryValue key

How to do it…

In the following example, we will create a dictionary containing a set of key/value pairs and then, using the dict get command, obtain the value associated with the specified key. 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 get $names 3
Paul

How it works…

The dict get command will retrieve the value associated with the argument contained in the key for the dictionary defined by

Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.