In this chapter, we will cover the following topics:
Strings hold textual data from a single character to a large text file. Lists allow us to store groups of strings and lists in an organized manner. But neither offers a simple method for relating data elements to a key value, in the manner of an array or database. If you need to organize multiple items under a single group, nothing beats a dictionary.
Dictionary allows storage of data with a key/value mapping methodology, with each key in the dictionary mapping to a single value. Dictionaries are textual in nature (like how lists are), but allow association between key/value pairs. For example, if I create a dictionary "Fruits" with a key of "Apple" and a value of "17"; I have the beginnings of a simple inventory system. With the addition of nested dictionaries, you can rapidly emulate data storage and retrieval similar to a database application without the overhead of a third-party product.
As with all things in Tcl, we have been provided with a command and a full complement of options to create and manage our dictionaries. In the following sections, we will cover both, the dict
command as well as its options. The dict
command is the workhorse for all of your dictionary needs. By pairing it with the optional keywords, you instruct the interpreter how to utilize the command. The syntax is as follows:
dict option argument1 argument2…
The dict
command performs a function based on the option defined. Argument number and type are predicated by the option that is selected. As a pre-requisite for the recipes in this chapter, launch the Tcl shell, as appropriate for your operating system. You can access Tcl from the command line to execute the commands.
18.219.71.21