Creating Associations
Associations are sort of like neatly-organized collections that allows you to look stuff up fast. If you’re familiar with programming terms, these also go by the names “Hash tables,” “Associative arrays,” “Hash maps,” or “Dictionaries.”
Here’s an example. Let’s say you have a terrible memory, and want to create a GuidedTrack program that can give you the birthday of any of your friends. You set things up so that all you have to do is type in their name, and their birthday pops up on the next screen.
With that scenario, you might use an association to add all the starting data you have. Each entry in an association must include two things:
- A “key” (the term associated with each piece of data, in this case the name of your friend)
- Its value (such as the friend’s birthday).
If you were a postal enthusiast, the keys of your next association might be thousands of zip codes, and the values might be the names of the town associated with each key/zip code.
The code for associations is a little different, so to start, here’s an association with just one birthday entry in it:
In this example, the name Justin is the key that you’ll use later to find the associated data, which in this case is Justin’s birthday, March 1, 1994.
What’s perhaps most striking here is that you’ll use {curly braces} instead of [square brackets] to create the association. Also, the arrow ( -> ) is used to say something along the lines of “when I input this key, show me this thing right here.”
The final program, once you’ve added more of your friends, might look like this:
Each entry in the association is separated by a comma. You can then type the name of your friend, saving your selection as “friend”, and then, similar to the way you would show a specific position in a collection, you display that friend’s birthday by typing {friendsBirthday[friend]}
You don’t always have to use a variable though to show items in an association. If you had Bieber fever, you could prominently display Justin’s birthday in your program by typing:
While you could accomplish this same feat using collections and the *while keyword, associations make it much easier to look up values associated with other values.
Adding or changing a single item in an association
Once you’ve got a basic association set up, it’s easy to add or modify items.
For example, let’s say you have an association of English words with their Polish translations. If you want to add a new word to this pre-existing association (which you’ve called polish_words), you can simply do the following:
“tea” will be a new key added to the polish_words association, and “herbata” will be its value.
Similarly, if you realized you misspelled something and need to replace the value for a given key, you can overwrite a key in similar fashion:
If “cake” was already in the association, it’s value is replaced by “ciasto.” If it wasn’t in the association, then it’ll be added new.
One more association example, for good measure
If we revisit the earlier example about adding friends’ birthdays to an association, there’s another way we may want to organize lots of info we have about our friends. For example:
In the above example, each friend has their own association, complete with different keys representing crucial details about them. The collection ‘myfriends’ contains all the associations. If you set things up this way, here’s a couple ways you could display data:
The first line accesses the second association in the myFriends collection and displays the value to the key “name,” which is “Mariah.”
The second line would display the key “Perfume” of friend3, which is the sensual “Eau de Gaga,” with its opulent woody-floral intensity.
You could display all your friends’ signature scents, like so:
|