Lua Session 02: Tables
The universal data structure. Tables are Lua’s only data structure - they serve as arrays, dictionaries, objects, and modules.
Pre-Session State
-
Know Lua basic types
-
Understand operators (
~=,..) -
Can write if/for blocks
Lesson 1: Tables as Arrays
Concept: Arrays in Lua are 1-indexed tables.
Exercise 1.1: Array basics
-- Arrays are 1-indexed!
local hosts = {"kvm-01", "kvm-02", "vault-01"}
print(hosts[1]) -- kvm-01 (NOT hosts[0])
print(hosts[3]) -- vault-01
print(#hosts) -- 3 (length)
Exercise 1.2: Array modification
local hosts = {"kvm-01", "kvm-02"}
-- Add to end
table.insert(hosts, "vault-01")
-- Add at position
table.insert(hosts, 1, "gateway")
-- Remove from position
table.remove(hosts, 1)
print(table.concat(hosts, ", "))
Exercise 1.3: Array iteration with ipairs
local hosts = {"kvm-01", "kvm-02", "vault-01"}
-- ipairs for arrays (ordered, numeric keys)
for i, host in ipairs(hosts) do
print(i .. ": " .. host)
end
Lesson 2: Tables as Dictionaries
Concept: Tables with string keys act as dictionaries.
Exercise 2.1: Dictionary basics
local server = {
name = "kvm-01",
ip = "10.50.1.110",
port = 22,
active = true
}
-- Dot access for string keys
print(server.name)
print(server.ip)
-- Bracket access (required for special keys)
print(server["port"])
Exercise 2.2: Dynamic keys
local server = {}
-- Add keys dynamically
server.name = "kvm-01"
server["ip-address"] = "10.50.1.110" -- Dash requires brackets
-- Variable as key
local key = "vlan"
server[key] = 10
print(server.vlan) -- 10
Exercise 2.3: Dictionary iteration with pairs
local server = {
name = "kvm-01",
ip = "10.50.1.110",
role = "hypervisor"
}
-- pairs for dictionaries (unordered)
for key, value in pairs(server) do
print(key .. ": " .. tostring(value))
end
Lesson 3: ipairs vs pairs
Concept: Use ipairs for arrays, pairs for dictionaries.
Exercise 3.1: ipairs stops at first nil
local t = {1, 2, nil, 4, 5}
print("ipairs (stops at nil):")
for i, v in ipairs(t) do
print(i, v) -- Only prints 1, 2
end
print("pairs (all non-nil):")
for k, v in pairs(t) do
print(k, v) -- Prints 1, 2, 4, 5
end
Exercise 3.2: Mixed table
local mixed = {
"first", -- [1]
"second", -- [2]
name = "kvm-01", -- string key
ip = "10.50.1.110" -- string key
}
print("Array part (ipairs):")
for i, v in ipairs(mixed) do
print(i, v) -- 1: first, 2: second
end
print("All keys (pairs):")
for k, v in pairs(mixed) do
print(k, v) -- All 4 entries
end
Lesson 4: Table Utilities
Concept: Built-in table functions.
Exercise 4.1: table.concat
local hosts = {"kvm-01", "kvm-02", "vault-01"}
print(table.concat(hosts, ", "))
-- kvm-01, kvm-02, vault-01
print(table.concat(hosts, "\n"))
-- Each on new line
Exercise 4.2: table.sort
local numbers = {3, 1, 4, 1, 5, 9}
table.sort(numbers)
print(table.concat(numbers, ", ")) -- 1, 1, 3, 4, 5, 9
-- Custom sort
local hosts = {"vault-01", "kvm-02", "kvm-01"}
table.sort(hosts)
print(table.concat(hosts, ", ")) -- kvm-01, kvm-02, vault-01
-- Sort by field
local servers = {
{name = "vault", cpu = 45},
{name = "kvm-01", cpu = 85},
{name = "kvm-02", cpu = 32}
}
table.sort(servers, function(a, b)
return a.cpu > b.cpu -- Descending
end)
Exercise 4.3: Check if table contains
-- Lua has no built-in "contains"
local function contains(t, value)
for _, v in ipairs(t) do
if v == value then return true end
end
return false
end
local hosts = {"kvm-01", "kvm-02"}
print(contains(hosts, "kvm-01")) -- true
print(contains(hosts, "vault")) -- false
Exercise 4.4: Shallow copy
-- Tables are passed by reference
local function copy(t)
local new = {}
for k, v in pairs(t) do
new[k] = v
end
return new
end
local original = {name = "kvm-01", ip = "10.50.1.110"}
local copied = copy(original)
copied.name = "kvm-02"
print(original.name) -- kvm-01 (unchanged)
print(copied.name) -- kvm-02
Summary: What You Learned
| Concept | Syntax | Example |
|---|---|---|
Array |
|
|
Dictionary |
|
|
1-indexed |
|
First element |
Length |
|
Array length only |
ipairs |
|
Arrays (ordered) |
pairs |
|
Dictionaries (unordered) |
Insert |
|
Add to end |
Concat |
|
Join to string |
Sort |
|
In-place sort |
Exercises to Complete
-
[ ] Create array of hostnames, iterate with ipairs
-
[ ] Create server dict with name, ip, vlan, cpu
-
[ ] Sort servers by CPU descending
-
[ ] Write a function to merge two tables
Next Session
Session 03: Neovim API - vim.api, vim.opt, vim.keymap.
Session Log
| Timestamp | Notes |
|---|---|
Start |
<Record when you started> |
End |
<Record when you finished> |