So far we have seen how programs can remember information by storing values in variables. The types of the values we have seen so far have one thing in common, they are all single values. What happens if we want to store a list of values? Lua has a compound data type called a table that can be used for this purpose.
Lets imagine we want to store a list of names for a class we are teaching on programming. In Lua we define a table to hold these names. Each name is a string value just like the ones we have seen before. Only now they are grouped together in one variable of type table.
-- Lists of values.
local names = { "Alice", "Bob", "Carol" }
io.write(names[1] .. "\n")
io.write(names[2] .. "\n")
io.write(names[3] .. "\n")
io.write("\n")
io.write(type(names) .. "\n")
io.write(type(names[1]) .. "\n")
The output of this program is as follows:
Alice
Bob
Carol
table
stringWe declare a local variable called names, and assign a table to it. The table is
denoted by the braces {}. The strings inside the braces are the values to be
contained by the table. They are each separated by a comma ,.
We print out each name from the table names. We can access each individual
value in a table by using the square brackets [] notation on the end of the
variable. The first value in a Lua table is accessed by the number 1.
We output the type of the names variable - table - and the first value in
the table - string.
-- Adding a name.
local names = { "Alice", "Bob", "Carol" }
table.insert(names, "Dave")
io.write(names[1] .. "\n")
io.write(names[2] .. "\n")
io.write(names[3] .. "\n")
io.write(names[4] .. "\n")
Lua has a set of methods that begin table. for manipulating a table variable.
We call the table.insert method, passing in the names variable and a new string
value to add to the end of the table (in position 4). The output of the program
should be as follows:
Alice
Bob
Carol
DaveSometimes we want to iterate over the whole set of values in a table. We can do
this using a different version of the for loop. This version doesn't take a
start and end value. Instead it uses the in keyword to assign each index and
value from the table to a couple of local variables (in this case i and v).
-- Looping over a list.
local names = { "Alice", "Bob", "Carol" }
for i, v in ipairs(names) do
io.write("Name " .. i .. " is " .. v .. "\n")
end
The output of this program is as follows:
Name 1 is Alice
Name 2 is Bob
Name 3 is Carol
If we just want the values and don't care about the index, we can use the discard
variable _ to tell Lua we don't care about it.
-- Looping over a list.
local names = { "Alice", "Bob", "Carol" }
for _, v in ipairs(names) do
io.write(v .. "\n")
end
We can use the # operator to get the size of a list like so:
-- Size of a list.
local names = { "Alice", "Bob", "Carol" }
io.write("Names is " .. #names .. " values long")
The output of this program is Names is 3 values long..
We can also remove a name from a table by calling table.remove like in this
listing.
-- Removing values from a list.
local names = { "Alice", "Bob", "Carol", "Dave" }
for _, v in ipairs(names) do
io.write(v .. ", ")
end
io.write("\n")
table.remove(names, 2)
for _, v in ipairs(names) do
io.write(v .. ", ")
end
io.write("\n")
The output of the above program is this:
Alice, Bob, Carol, Dave,
Alice, Carol, Dave,
There are more operations we can perform on tables when using them as lists. In the next lesson we will see how we can find out what these operations (and others) are by looking at Lua's standard library.
Create a program that writes out a list of names backwards. Recall the earlier
exercise using a for loop we saw in another lesson that let us enumerate numbers
in reverse order. Combine this with the size operator # and square brackets
access [] to complete this task.
Create a variable names that has as its value an empty table.
-- Looping over a list backwards.
local names = { "Alice", "Bob", "Carol" }
for i = #names, 1, -1 do
io.write(names[i] .. "\n")
end
local names = {}.