Rob Cook


Home | GitHub | Dev.to | Donate | Contact

[8] A little bit of Lua


2026-01-12

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.

Taking register

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
string

Line by line walkthrough

Adding a new student

-- 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
Dave

Listing all students

Sometimes 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

Getting the number of students

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..

Removing a student

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.

Exercises

  1. 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.

  2. Create a variable names that has as its value an empty table.

Answers

  1. This is a tricky one that combines a number of things you have seen. Well done if you got it working.
-- Looping over a list backwards.

local names = { "Alice", "Bob", "Carol" }

for i = #names, 1, -1 do
  io.write(names[i] .. "\n")
end
  1. This one is easy, local names = {}.
end