Lua Basics

Variables and Types

Variables and scoping
-- Local variables (always prefer local)
local name = "Evan"
local port = 8080
local connected = true

-- Global (avoid -- pollutes _G)
hostname = "modestus-aw"       -- global by default without local

-- Multiple assignment
local x, y, z = 1, 2, 3
local a, b = "hello"           -- b is nil

-- Swap without temp
x, y = y, x
Core types — Lua has 8
type(42)              -- "number"   (double-precision float; integers in 5.3+)
type("hello")         -- "string"
type(true)            -- "boolean"
type(nil)             -- "nil"      (absence of value; falsy)
type({})              -- "table"    (the universal data structure)
type(print)           -- "function"
type(io.stdin)        -- "userdata"
type(coroutine.create(function() end))  -- "thread"

-- Only nil and false are falsy
-- 0, "", and {} are ALL truthy (unlike Python/JS)
if 0 then print("0 is truthy in Lua") end
if "" then print("empty string is truthy") end

Control Flow

Conditionals and loops
-- if/elseif/else (note: elseif, not elif or elsif)
if status == "active" then
  print("running")
elseif status == "degraded" then
  print("warning")
else
  print("down")
end

-- No switch/case -- use elseif chains or table lookup
local actions = {
  active   = function() return "green" end,
  degraded = function() return "yellow" end,
  down     = function() return "red" end,
}
local color = (actions[status] or function() return "unknown" end)()

-- Numeric for
for i = 1, 10 do print(i) end           -- 1 to 10
for i = 10, 1, -1 do print(i) end       -- countdown
for i = 0, 100, 5 do print(i) end       -- step by 5

-- Generic for (iterators)
for k, v in pairs(config) do             -- unordered key-value
  print(k, v)
end
for i, v in ipairs(list) do              -- ordered integer keys
  print(i, v)
end

-- While and repeat
local retries = 0
while retries < 3 do
  retries = retries + 1
end

repeat
  retries = retries - 1
until retries == 0                       -- condition checked after body

Strings

String operations
local s = "Hello, World"
#s                                       -- length: 12
string.len(s)                            -- same: 12
string.upper(s)                          -- "HELLO, WORLD"
string.lower(s)                          -- "hello, world"
string.sub(s, 1, 5)                      -- "Hello" (1-indexed, inclusive)
string.rep("=-", 20)                     -- repeat 20 times
string.reverse(s)                        -- "dlroW ,olleH"

-- Concatenation
local full = "host" .. "-" .. "01"       -- "host-01"

-- Format (printf-style)
string.format("%-20s %5d", hostname, port)
string.format("IP: %s Port: %d", "10.50.1.40", 8200)

-- Find and match
string.find(s, "World")                  -- 8, 12 (start, end)
string.match(s, "(%w+)")                 -- "Hello" (first capture)

-- Multi-line strings
local block = [[
  This is a multi-line
  string literal (no escaping needed)
]]

Operators

Comparison, logical, arithmetic
-- Comparison: ==  ~=  <  >  <=  >=
-- Note: ~= is not-equal (not != like most languages)

-- Logical: and, or, not (short-circuit)
local result = value or "default"        -- idiomatic default
local name = first_name and first_name or "unknown"

-- Arithmetic: + - * / % ^
-- Integer division (Lua 5.3+): //
local half = 10 // 3                     -- 3

-- String coercion
"10" + 5                                 -- 15 (auto-converts)
tostring(42)                             -- "42"
tonumber("42")                           -- 42