Web Development With HHVM and Hack 6: Dictionaries

Mike Abelar
4 min readFeb 25, 2020

--

In the last tutorial, we covered vectors: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-5-vectors-d982d62bf105. In this tutorial, we will cover dictionaries.

What Are Dictionaries?

Dictionaries allow us to use a key to get a value. Consider the example of the English dictionary. Let’s pretend we have been presented the following word that we do not know the definition of: “Gobbledygook”. Our natural instinct is to look the word up in a dictionary. The dictionary would then tell us that the word Gobbledygook means gibberish. Dictionaries in programming do the same thing: they take a key and give us a value.

Dictionaries Example

Let us create a new folder for this tutorial and inside the folder create a new hhconfig file:

touch .hhconfig

Then, create a new Hack file to play around with dictionaries:

touch dictionaries.hack

Inside the file, paste the following contents:

<<__EntryPoint>>
function main(): noreturn {
// code here
$words = dict[
"gobbledygook" => "gibberish",
"astonish" => "surprise"
];
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
exit(0);
}

Let’s type check the code to make sure it is good to run: hh_client dictionaries.hack . We see that there are no errors, so we are clear to run the program:

hhvm dictionaries.hack

We get the following output:

Word: gobbledygook Definition: gibberish
Word: astonish Definition: surprise

Let us look through the code. First:

$words = dict[
"gobbledygook" => "gibberish",
"astonish" => "surprise"
];

In this portion, we define our dictionary. In this case, we are creating a variable called words , which is a dictionary that maps a word to a definition, similar to what we did above. We create the dictionary with the keyword dict followed by square brackets. We then define the key to value relationship:

"gobbledygook" => "gibberish"

Where “gobbledygook” is the key and “gibberish” is the value. We separate key-value pairs with a comma.

We then get to the foreach:

foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}

Here, we want to iterate through each key-value pair. We do this by using $word => $definition following the as keyword. Below, we can then print out the word and the definition for each key-value pair.

We are also able to directly access the value from a dictionary with a key by doing the following:

$words["gobbledygook"]

Therefore, print($words["gobbledygook"]); would print “gibberish”

We can also have dictionaries that map from integers to strings:

<<__EntryPoint>>
function main(): noreturn {
// code here
$words = dict[
2 => "gibberish",
1 => "surprise"
];
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
exit(0);
}

which prints:

Word: 2 Definition: gibberish
Word: 1 Definition: surprise

The values can be of any type you want. The keys must either be integers or strings.

Additionally, we can mix the types of key-value pairs:

<<__EntryPoint>>
function main(): noreturn {
// code here
$words = dict[
"gobbledygook" => "gibberish",
1 => "surprise"
];
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
exit(0);
}

which prints:

Word: gobbledygook Definition: gibberish
Word: 1 Definition: surprise

Adding To a Dictionary:

In the following example, we add a key-value entry to a dictionary:

<<__EntryPoint>>
function main(): noreturn {
// code here
$words = dict[
"gobbledygook" => "gibberish",
"astonish" => "surprise"
];
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
// adds a new word
$words["new"] = "word";
print("===============\n");
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
exit(0);
}

Here, we assign the key string “new” to have a value of “word”.

Which prints:

Word: gobbledygook Definition: gibberish
Word: astonish Definition: surprise
===============
Word: gobbledygook Definition: gibberish
Word: astonish Definition: surprise
Word: new Definition: word

Removing a Key

In the following example, we remove a key-value entry from a dictionary:

<<__EntryPoint>>
function main(): noreturn {
// code here
$words = dict[
"gobbledygook" => "gibberish",
"astonish" => "surprise"
];
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
unset($words["gobbledygook"]);
print("===============\n");
foreach ($words as $word => $definition) {
print("Word: " . $word . " Definition: " . $definition . "\n");
}
exit(0);
}

Here, we put $words[gobbledygook] inside of unset() , which removes the entry

Which prints:

Word: gobbledygook Definition: gibberish
Word: astonish Definition: surprise
===============
Word: astonish Definition: surprise

In the next tutorial, we cover functions: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-7-functions-7a0f56023324

--

--