Getting a list of keys

In order to minimize effort on dictionary maintenance Tcl has provided the dict keys command to return a list of keys that exist within a dictionary. The syntax is as follows:

	dict keys dictionaryValue pattern

How to do it…

In the following example we will create a dictionary containing a collection of key/value pairs and then using the dict keys command obtain a list of valid keys. 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 keys $test
1 2 3

How it works…

The dict keys command returns a list of keys that exists within the dictionary referenced by dictionaryValue. If a pattern is supplied, only those keys that ...

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.