Web Development With HHVM and Hack 5: Vectors

Mike Abelar
3 min readFeb 24, 2020

--

In this tutorial, we will cover vectors. In the previous tutorial, we covered conditional statements: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-4-conditional-statements-b212a7f65af8

What Are Vectors

Vectors allow you to store more than one expression in a single variable. In other words, vectors serve as lists.

Examples

Let us explore vectors by creating a new folder for this tutorial and a hhconfig file:

touch .hhconfig

Then, let’s create a file to play around with vectors:

touch vectors.hack

Inside the file, paste the following contents:

<<__EntryPoint>>
function main(): noreturn {
// code here
$names = vec["mike", "joe", "bill"];
print($names[0] . "\n");
print($names[1] . "\n");
print($names[2] . "\n");
exit(0);
}

Let’s run this code:

hhvm vectors.hack

To get:

mike
joe
bill

In this example, we have a vector of names, which serves a list of names containing “mike”, “joe”, and “bill”. We name this variable names . Let’s take a look at the syntax of how we made the vector:

$names = vec["mike", "joe", "bill"];

To declare a vector, we use the vec keyword followed by square brackets. Inside the square brackets, we populate the initial elements. Each element is separated by a comma. If we wanted to declare an empty vector, we can leave the square brackets empty:

$names = vec[];

We can also mix types in vectors. We can have vectors compose of numbers and strings. The code below is a valid use of vectors:

<<__EntryPoint>>
function main(): noreturn {
// code here
$names = vec["mike", 1, "bill"];
print($names[0] . "\n");
print($names[1] . "\n");
print($names[2] . "\n");
exit(0);
}

The code will print

mike
1
bill

Indexing

Let’s dive into the next part of the program:

 print($names[0] . "\n");
print($names[1] . "\n");
print($names[2] . "\n");

By running the code, we know that this code prints out all three elements of $names . Let’s take a look at one of these lines:

print($names[0] . "\n);

When we call $names[0] , we are indexing into the names vector and selecting the first element. Indexing is accessing a particular element from the vector. We index by using square brackets and a number to indicate the index we want. Note: vectors are zero-indexed. Meaning, the first element is accessed with [0] while the second element is accessed with [1] and so on. This means that if a vector has n elements, then the last element can be indexed with index n-1 .

Foreach Loops

Another way to access vectors is by using a foreach loop.

Below is an example of a foreach loop:

<<__EntryPoint>>
function main(): noreturn {
// code here
$names = vec["mike", "joe", "bill"];
foreach ($names as $name) {
print($name . "\n");
}
exit(0);
}

You will notice it prints the same thing as our first example. A foreach loop goes through each element in the vec and does something with it. Let’s look at code:

 foreach ($names as $name) {
print($name . "\n");
}

We create a foreach loop by adding parentheses after the foreach keyword. We start with the name of the vec, $names . Then we use as $name to specify that each element that we iterate over will be accessible by the variable called $name . Inside the block of code, specified by curly brackets, we print each name by using the $name variable.

Adding Elements

Adding an element to a vector has the following notation:

$names[] = "john";

The code above, adds “john” to the vector of names.

Let’s take a look an example:

<<__EntryPoint>>
function main(): noreturn {
// code here
$names = vec["mike", "bill"];
foreach ($names as $name) {
print($name . "\n");
}
print("=========\n");
$names[] = "greg";
foreach ($names as $name) {
print($name . "\n");
}
exit(0);
}

which prints

mike
bill
=========
mike
bill
greg

In the next tutorial, we cover dictionaries: https://medium.com/@mikeabelar/web-development-with-hhvm-and-hack-6-dictionaries-f951c9a557f7

--

--