[edit] Storing and retrieving data from arrays
Arrays and hashes are essentially variables which hold multiple data. The variables we have dealt with so far, are known as scalar variables. These are variables where they only hold one chunk of data, as opposed to holding several. We will start with the array first. You can think of an array as a series of containers to hold data, where each of the container is indexed and accessible separately. For example, a train has several cars tied together, and each car can hold people, boxes, coal, etc. Let's take a look at an array example.
[% train = [5, 7, 2]; "There are ${train.0} people in the first car.\n"; "There are ${train.1} people in the second car.\n"; "There are ${train.2} people in the third car.\n"; %]
The example above will result in the output below:
There are 5 people in the first car. There are 7 people in the second car. There are 2 people in the third car.
In this example, we have utilized TT to create an array to hold information regarding passengers on a train. The first line creates an array named train with 3 containers, with numbers 5, 7, and 2. An array is fined by brackets [] surrounding the containers while commas , separate the individual containers.
Now that we have defined the variable train defined, with each container (or cell) populated, we will be accessing each cell. Each cell is indexed (or labeled) with a number for easy access. Arrays in TT begin the first cell with the index 0. So if you were to access the first cell, you would get the cell indexed with a 0. The next cell trailing after cell 0 would have 1 has the index, and so on. So for our train array, it holds the number 5 in cell 0, 7 in cell 1 and finally 2 in cell 2.
The next three line that follow the array creation, retrieves the data held in the cells and prints them out in a sentence. We are embedding the content of the variables into string via interpolation as covered in the previous chapter. The code ${train.0} in the first string is replaced with the first cell content of train, which is 5. So the first string gets evaluated as "There are 5 people in the first car." The remaining lines perform the same evaluation with just different cells.
You can also add to an array, or even update the data of a particular cell. Let's take a look at an example:
[% train = [5, 7, 2]; train.1 = 4; train.3 = 8; "There are ${train.0} people in the first car.\n"; "There are ${train.1} people in the second car.\n"; "There are ${train.2} people in the third car.\n"; "There are ${train.3} people in the fourth car.\n"; %]
The example above will result in the output below:
There are 5 people in the first car. There are 4 people in the second car. There are 2 people in the third car. There are 8 people in the fourth car.
So taking the analogy of a train, we have done two new things to the train. The first new line train.1 = 4; changes the number of people in the second car to 4 (remember that the index of cells for arrays start from 0 instead of 1). The second new line, although it looks similar to the first line, it is actually adding a fourth car with 8 people to the train. Now that the train has 4 cars, there is another new line to print the number of people in the fourth car.
Now what if we want to find how many cars there for the train, or more specifically, how many cells there are for an array? To find the size of an array, we are going to attach the function size to the array as such:
[% train = [5, 7, 2]; train.1 = 4; train.3 = 8; "There are ${train.size} cars for the train."; "There are ${train.0} people in the first car.\n"; "There are ${train.1} people in the second car.\n"; "There are ${train.2} people in the third car.\n"; "There are ${train.3} people in the fourth car.\n"; %]
The example above will result in the output below:
There are 4 cars for the train. There are 5 people in the first car. There are 4 people in the second car. There are 2 people in the third car. There are 8 people in the fourth car.
Simply adding the word size after the variable train returns the size of the array. size is a TT function that performs a specific task (in this case, it grabs the size of an array, then return it as a number value). Let's cover some other functions or methods that pertains to array variables.
The array is sometimes referred to as a stack, because they can be thought of as a stack of data or cells in a linear fashion. We will be looking at array functions that will add or remove cells from the beginning and the end of the arrays. Let's start out with a slightly different code this time:
[% train = ['electronics', 'people', 'toys']; "There are ${train.size} cars for the train."; "There are ${train.0} in the first car.\n"; "There are ${train.1} in the second car.\n"; "There are ${train.2} in the third car.\n"; %]
The example above will result in the output below:
There are 3 cars for the train. There are electronics in the first car. There are people in the second car. There are toys in the third car.
This example serves to show that you can store strings into an array as well. You can store anything in an array such as numbers, strings, arrays and hashes, but we will get to that in the upcoming chapters. For now, we will be focusing on modifying arrays, and look at some array functions that can add cells to the beginning of an array, and also at the end of an array. Let's say that the train will be transporting a car full of coal and another car full of dried mangos, yet the owner of the coal and the dried mangos insisted on transporting them as far apart as possible. So a compromise was reached with the conclusion of the transporting the coal in the front and the dried mangos at the end of the train.
[% train = ['electronics', 'people', 'toys']; train.unshift('coal'); train.push('dried mangos'); "There are ${train.size} cars for the train."; "There are ${train.0} in the first car.\n"; "There are ${train.1} in the second car.\n"; "There are ${train.2} in the third car.\n"; "There are ${train.3} in the fourth car.\n"; "There are ${train.4} in the fifth car.\n"; %]
The example above will result in the output below:
There are 5 cars for the train. There are coal in the first car. There are electronics in the second car. There are people in the third car. There are toys in the fourth car. There are dried mangos in the fifth car.
This example builds on top of the previous example, with the train array, filled with 'electronics', 'people' and 'toys'. The first new line uses the unshift function to add the string 'coal' to the front of the array. The syntax arrayname.push(itemtoadd) is use for the unshift function. The content between the parenthesis that follows the unshift function is what gets added to the front. The unshift function changes the index of every cell because there is a new cell in front, which is now the new 0 index cell. As a result, the index of the other cells gets bumped up one. So in our example, people which used to have an index of 1, now has a new index of 2 after the unshift.
The second new line with the push function, adds a new cell to the end of the array. This function is used just like the unshift function, arrayname.push(itemtoadd). Unlike unshift, push does not change the index of the existing cells because the cell being added is appended to the end of the array. Now that we know how to add a cell to the beginning and the end of an array, let us find out how we remove them.
[% train = [ 'coal', 'electronics', 'people', 'toys', 'dried mangos' ]; first = train.shift; last = train.pop; "There are ${train.size} cars for the train."; "There are ${train.0} in the first car.\n"; "There are ${train.1} in the second car.\n"; "There are ${train.2} in the third car.\n"; "The first removed car contains ${first}.\n"; "The last removed car contains ${last}.\n"; %]
The example above will result in the output below:
There are 3 cars for the train. There are electronics in the first car. There are people in the second car. There are toys in the third car. The first removed car contains coal. The last removed car contains dried mangos.
Instead of defining an array with 3 items, we have defined an array with 5 items for this example, the same 5 that were created in the previous example. We have two new functions this time, shift and pop. The line first = train.shift; does two things. First, it removes the first cell from the array train. Second, it stores the item removed from the front (in this case, 'coal') and stores it into a newly created variable called first. Because shift removes a cell from the beginning of an array, all the indices of the remaining cells are changed to one less than what they were before. So the value 'electronics', which used have an index of 1 prior to the shift, got a new index of 0 afterwards.
The next line, last = train.pop;, removes the very last cell from the array train and stores the removed cell into a newly created variable last. The train array now only has 3 cells, while the variables first and last contains 'coal' and 'dried mangos' respectively. There are other array functions available in TT, but they will not be covered here. Instead, Click Here to view them.