Rob Cook


Home | GitHub | Dev.to | Donate | Contact

[9] A little bit of Lua


2026-01-12

Even with a language as small as Lua, it is hard for a tutorial introduction such as this to mention everything about it. So how is an aspiring programmer to learn about the things they don't know? The answer is the Lua Reference Manual.

Section 6 - the standard library

Section 6 of the manual has a listing of the features of Lua's standard library. So far we have touched on the table section (with table.insert and table.remove in the previous lesson.). As you can see, there is a lot more to the library than just tables!

Take a look at the sections on tables and strings. The language and examples are quite terse, but it is enough to point you in the direction of a new feature. Lets look at table.concat in practice.

-- Concatenating a list to a string.

local names = { "Alice", "Bob", "Carol" }
local concatenated_names = table.concat(names, ", ")
io.write(concatenated_names .. "\n")

Outputs:

Alice, Bob, Carol

So that's one new function we have learnt from the table library! What about something from the string library?

-- Length of a string.

local message = "Hello, world!"
local length = string.len(message)
io.write(message .. " is " .. length .. " characters long.\n")

Outputs:

Hello, world! is 13 characters long.

Exercises

  1. Find a new function in the table or string libraries, and try to use it in a program.

Answers

  1. There are many answers to this one, but here is a program that uses table.sort to order a list of numbers in ascending order.
-- Sorting a list.

local numbers = { 100, 1, 10, 11 }
table.sort(numbers)

for _, v in ipairs(numbers) do
  io.write(v .. ", ")
end

io.write("\n")

Outputs:

1, 10, 11, 100, 
end