A dictionary is a special kind of collection. With a regular array, you must index it with integers. With dictionaries, you can index it with any object at all. Dictionaries thus provide a very powerful way of correlating one piece of information to another. Their only downside is that they are somewhat less efficient than simple arrays. Try the following:
y := Dictionary new y at: 'One' put: 1 y at: 'Two' put: 2 y at: 1 put: 'One' y at: 2 put: 'Two'
This fills our dictionary in with some data. The data is
actually stored in pairs of key and value (the key is what
you give to
at:—it specifies a slot; the value is what is
actually stored at that slot). Notice how we were able to
specify not only integers but also strings as both the key
and the value. In fact, we can use any kind of object we
want as either—the dictionary doesn’t care.
Now we can map each key to a value:
y at: 1 y at: 'Two'
which prints respectively:
We can also ask a dictionary to print itself:
Dictionary (1->'One' 2->'Two' 'One'->1 'Two'->2 )
where the first member of each pair is the key, and the second the value. It is now time to take a final look at the objects we have created, and send them to oblivion:
The exclamation mark deleted GNU Smalltalk’s knowledge of both
variables. Asking for them again will return just