Let's take a break from Pbinds and their ilk to talk about a data type. A Dictionary is a data type like an array, except that instead of using integers for indexes, you can use anything that you want.
( var id; id = Dictionary.new; id.put(\foo, "bar"); id[\foo].postln; )
The indexes are called keys and the items associated with them are called values. In the above example, \foo is the key and "bar" is the value. As mentioned above, you can use any kind of object for keys. And, like arrays, any kind of object can be the value.
( var id; id = Dictionary.new; id.put(\foo, "bar"); id.put(15, true); id.put("baz", 12.5); id[15].postln; )
You can get a list of the keys with the message keys, and then use that to step through a do loop:
( var id, keys; id = Dictionary[ \foo -> "bar", 15 -> true, "baz" -> 12.5 ]; keys = id.keys; keys.postln; keys.do({|key| "key is %, value is %".format(key, id.at(key)).postln; }); )
In that example, we see another way of declaring a Dictionary, with square brackets and key, value pairs in the format key -> value
We also see a new way of formatting strings. If you want to print a string with some variables in it, you can use % to signify where the variable should go and then pass the variables as arguments to format. You can use as many variables as you would like: "string %, % and %".format(var1, var2, var3)
Notice that the keys are in a random order. If you want them in a particular order, you will have to sort them.
Here's another example:
( var triangle_spectrum, freqs; triangle_spectrum = Dictionary[ 1->1, 3->(1/9), 5->(1/25), 7->(1/49), 9->(1/81) ]; freqs = triangle_spectrum.keys; freqs = freqs.asArray; freqs = freqs.sort; freqs.do({|freq| "freq %\t amplitude %\n".postf(freq * 440, triangle_spectrum[freq]); }) )
This stores the (non-normalised) spectrum of a triangle wave. Note that when we declare the Dictionary, we need to put mathematical expressions inside parentheses.
Then, we get the keys from the dictionary. Then we need to convert that Set to an Array. We can then tell the Array to sort itself.
\t means tab and \n means newline. We use these for formatting, to make our output look nice. postf formats a string and then posts it. It does not include a newline, so we do it ourselves with a \n
Array.sort takes an optional argument, which is a sort function. Here's an example to do a reverse sort:
( var triangle_spectrum, freqs; triangle_spectrum = Dictionary[ 1->1, 3->(1/9), 5->(1/25), 7->(1/49), 9->(1/81) ]; freqs = triangle_spectrum.keys; freqs = freqs.asArray; freqs = freqs.sort({|a, b| a > b}); freqs.do({|freq| "freq %\t amplitude %\n".postf(freq * 440, triangle_spectrum[freq]); }) )
The sort function takes two agruments and returns a boolean. In the above example, it compares two items and if the first one is bigger, it returns true. This will order the array so that larger items come first.
We can check to see if a Dictionary includes a particular key:
( var triangle_spectrum; triangle_spectrum = Dictionary[ 1->1, 3->(1/9), 5->(1/25), 7->(1/49), 9->(1/81) ]; triangle_spectrum.includesKey(4).postln; )
We can also look for the key of a particular item:
( var id; id = Dictionary[ \foo -> "bar", 15 -> true, "baz" -> 12.5 ]; id.findKeyForValue(12.5).postln; )
Summary
- A Dictionary is made up of key, value pairs.
- You can add items to a Dictionary with a message put(key, value)
- Dictionaries can be declared with key -> value pairs in square brackets.
- Dictionary.keys exports a set of the keys, in random order.
- If you want to sort the keys, you must pass them the message .asArray and then pass the message sort to the result of that.
- If you want to sort anyhting besides numbers from small ot large you have to write a sort function
- A sort function takes two arguments and returns a boolean based on a comparison between them.
- You can test is an Dictionary contains a particular key with includesKey
- You can look up the key for a value with findKeyForValue