Associations (AKA Dictionaries, Hash Maps, Hash Tables, or Associative Arrays)🔗

hash table, hash map, dictionary, associative array, data structure

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:

  1. A "key" (the term associated with each piece of data, in this case the name of your friend)
  2. 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).

Let's go through the syntax of an association. To start, here's an association with just one birthday entry in it:

>> friendsBirthdays = {"Justin" -> "March 1, 1994"}

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.

Notice that associations are surrounded by {curly braces}. Also, the arrow (->) implies that the key on the left of the arrow is linked to the data to the right of the arrow. In order to enter more key-data pairs, simply separate them with a comma. The final program, once you've added more of your friends, might look like this:

>> friendsBirthdays = {"Justin" -> "March 1, 1994", "Kim" -> "October 21, 1980", "Taylor" -> "December 13, 1989", "Beyonce" -> "September 4, 1981", "Michael" -> "August 29, 1958"}

*label: top
*question: Which friend's birthday do you need?
	*save: friend

{friendsBirthdays[friend]}
*goto: top

You can then type the name of your friend, saving your selection as friend, and then display that friend's birthday by typing {friendsBirthday[friend]}.

You don't always have to use a variable to show items in an association. If you had Bieber fever, you could prominently display Justin's birthday in your program by typing:

{friendsBirthdays["Justin"]}

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:

>> polish_words["tea"] = "herbata"

The new key "tea" will be 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:

>> polish_words["cake"] = "ciasto"

If "cake" was already in the association, its value is replaced by "ciasto." If it wasn't in the association, then it'll be added now.

Looping through all the elements of an association🔗

Use the *for keyword to go through all the elements of your association:

>> friendsBirthdays = {"Justin" -> "March 1, 1994", "Kim" -> "October 21, 1980", "Taylor" -> "December 13, 1989", "Beyonce" -> "September 4, 1981", "Michael" -> "August 29, 1958"}

*header: My friend's birthdays
*for: name, date in friendsBirthdays
	{name} was born on {date}

Here on each iteration we retrieve an element of the association, name is assigned the key of the element and date, its value. This is what you see on the screen when running the code:

Looping through all the elements of an association (1)

Alternatively, you may only want to get the value of each element, without the key. Here is how you can do that:

>> potluckSignup = {"Peter" -> "Deviled eggs", "Joan" -> "Hummus and carrots", "Kelly" -> "Cookies", "Andrew" -> "Pico de gallo"}

*header: Potluck dishes
*for: dish in potluckSignup
	- {dish}

In summary, when you are using *for to loop through the elements of an association, you can either retrieve the key and value of each element using the syntax *for: key, value in association or just the value if you write *for: value in association.

Using a collection of associations🔗

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:

>> friend1 = {"name" -> "Kim", "Birthday" -> "October 21, 1980", "Perfume" -> "Pure Honey"}
>> friend2 = {"name" -> "Mariah", "Birthday" -> "March 27, 1970", "Perfume" -> "Lollipop Bling"}
>> friend3 = {"name" -> "Lady", "Birthday" -> "March 28, 1986", "Perfume" -> "Eau de Gaga"}

>> myFriends = [friend1, friend2, friend3]

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:

{myFriends[2]["name"]}

{friend3["Perfume"]}

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:

*for: friend in myFriends
	{friend["Perfume"]}

Get all the keys to your association🔗

If you want a collection of all the keys in your association (e.g. all the Polish words for which you have English translations), you can use .keys, like so:

>> myWords = {"bee" -> "pszczoła", "beetle" -> "chrząszcz", "book" -> "książka"}

The words you know are: {myWords.keys}

The example above would return, "The words you know are: bee, beetle, book".


Next: