Lua is a lightweight, high-performance, and embeddable programming language that is widely used for scripting and automation. It is known for its simple and yet powerful syntax, making it a great language for beginners and experts alike. Lua is often used as an embedded scripting language, but it can also be used as a standalone language.
Getting Lua
Windows
- Visit the official Lua website at lua.org.
- Download the precompiled binaries for Windows from the download page.
- Extract the downloaded archive to a directory of your choice.
- Add the Lua executable (
lua.exe) to your system’s PATH environment variable. - Open a command prompt and type
luato start the Lua interpreter.
Linux
- Most Linux distributions have Lua available in their package repositories.
- For Debian-based systems like Ubuntu, install Lua using
sudo apt-get install lua5.3. - For Red Hat-based systems like Fedora, use
sudo dnf install lua. - Alternatively, you can download the source from lua.org and compile it yourself.
- Open a terminal and type
luato start the Lua interpreter.
MacOS
- Lua can be easily installed using package managers like Homebrew or MacPorts.
- If using Homebrew, install Lua by running
brew install luain the terminal. - If using MacPorts, run
sudo port install lua. - Alternatively, download the source from lua.org and compile it.
- Open the Terminal app and type
luato start the Lua interpreter.
Basics of Lua
Hello World
--Prints Hello world on the console. Also double dashes like this are comments
print("Hello, World!")Variables and Data Types
Lua has a small set of basic data types: nil, boolean, number, string, table, function, userdata, and thread.
-- Variables
local a_number = 42
local a_boolean = true
local a_string = "Lua is great"
-- Tables (similar to arrays or hash tables in other languages. Starts at index 1 instead of 0)
local a_table = {key1 = "value1", key2 = "value2"}
-- Concatenating strings
local a_string_two = " isnt it?"
print(a_string .. a_string_two)
-- prints "Lua is great isnt it?"Operators
Lua supports the usual arithmetic (+, -, *, /, %, ^, //) and logical operators (and, or, not), as well as relational operators (==, ~=, <, >, <=, >=).
Control Structures
-- If statement
if a_number > 0 then
print("Positive number")
elseif a_number < 0 then
print("Negative number")
else
print("Zero")
end
-- While loop
while a_number > 0 do
print(a_number)
a_number = a_number - 1
end
-- For loop
for i = 1, 10 do
print(i)
end
-- Repeat until loop
repeat
print(a_number)
a_number = a_number + 1
until a_number > 10Functions
-- Define a function
function greet(name)
print("Hello, " .. name .. "!")
end
-- Call the function
greet("Alice")Tables
Tables are the primary data structuring mechanism in Lua and can represent arrays, lists, maps, sets, and more.
-- Create a table
local fruits = {"apple", "banana", "cherry"}
-- Access table elements
print(fruits[1]) -- Lua tables are 1-indexed
-- Add to a table
fruits[4] = "date"
-- Iterate over a table
for i, fruit in ipairs(fruits) do
print(i, fruit)
endInput and Output
-- Read input from the user
io.write("Enter your name: ")
local name = io.read()
print("Hello, " .. name .. "!")
-- Write output to the console
print("This is a message")File I/O
-- Open a file
local file = io.open("example.txt", "w")
-- Write to the file
file:write("Hello, File World!")
-- Close the file
file:close()Error Handling
-- Using pcall to handle errors
local success, result = pcall(function()
error("This is an error")
end)
if success then
print(result)
else
print("Error: " .. result)
endRunning Lua Scripts
To run a Lua script, save your code in a file with a .lua extension, for example script.lua. Then, execute it from the command line:
lua script.luaEmbedding Lua
Lua is often embedded into other applications to provide scripting functionality. This is done by linking the Lua library into the application and providing an interface for the application to call Lua functions and vice versa.
For more on Lua, check Lua Docs, they have a book available there. You might also want to check LuaRocks for Modules.




