Lua
Programming Language
Lua
Overview
Lua is a lightweight and fast embedded scripting language used in game development, embedded systems, and configuration scripts.
Details
Lua is a lightweight programming language developed by a Brazilian research group in 1993. "Lua" means "moon" in Portuguese. Despite being a very small interpreter of only a few dozen kilobytes, it is known for its powerful capabilities. Implemented in C, it can be easily embedded into C/C++ applications, making it primarily used as an extension language for other applications. It is particularly popular in the gaming industry, being used in many famous games such as World of Warcraft, Angry Birds, and Roblox. It features simple and intuitive syntax, dynamic typing, automatic memory management, and powerful table functionality, achieving high performance despite its small footprint. It is utilized in a wide range of fields including embedded systems, configuration files, and network device scripts.
Usage Examples
Hello World
-- Basic output
print("Hello, World!")
-- Multiple value output
print("Hello,", "Lua!")
-- Output using variables
local message = "Hello, Lua!"
print(message)
-- String concatenation
local name = "John"
local age = 25
print("My name is " .. name .. " and I am " .. age .. " years old.")
-- Formatted string using string.format
local formatted = string.format("My name is %s and I am %d years old.", name, age)
print(formatted)
-- Function example
function greet(name)
return "Hello, " .. name .. "!"
end
print(greet("Smith"))
-- Local function
local function createMessage(name, age)
return string.format("Name: %s, Age: %d", name, age)
end
print(createMessage("Johnson", 30))
-- Multi-line string
local multiline = [[
This is a multi-line
string.
In Lua, use [[]] to enclose.
]]
print(multiline)
-- Conditional output
local debug = true
if debug then
print("Debug mode is enabled")
end
-- Loop example
for i = 1, 5 do
print("Hello " .. i .. " times!")
end
-- Table example
local fruits = {"apple", "banana", "orange"}
for i, fruit in ipairs(fruits) do
print(i .. ". " .. fruit)
end
-- Time display
print("Current time: " .. os.date("%Y-%m-%d %H:%M:%S"))
-- File output example
local function writeToFile()
local file = io.open("hello.txt", "w")
if file then
file:write("Hello, World from Lua!\n")
file:write("Multi-language support available\n")
file:close()
print("Written to file: hello.txt")
else
print("Failed to create file")
end
end
writeToFile()
-- Execution as standalone script
if arg and arg[0] then
print("Script file: " .. arg[0])
if arg[1] then
print("Argument 1: " .. arg[1])
end
end
Variables and Data Types
-- Lua has local and global variables
-- Without 'local' keyword, variables become global
-- Basic data types
local stringValue = "string"
local numberValue = 42
local floatValue = 3.14159
local booleanValue = true
local nilValue = nil
print("=== Basic Data Types ===")
print("String:", stringValue, type(stringValue))
print("Integer:", numberValue, type(numberValue))
print("Float:", floatValue, type(floatValue))
print("Boolean:", booleanValue, type(booleanValue))
print("Nil:", nilValue, type(nilValue))
-- In Lua, integers and floats are not distinguished (distinguishable from Lua 5.3+)
local int = 10
local float = 10.0
print("Is integer?:", math.type and math.type(int) or "number")
print("Is float?:", math.type and math.type(float) or "number")
-- String operations
local str1 = "Hello"
local str2 = "World"
local combined = str1 .. " " .. str2 -- String concatenation
print("Concatenated:", combined)
print("Length:", #combined)
print("Uppercase:", string.upper(combined))
print("Lowercase:", string.lower(combined))
print("Substring:", string.sub(combined, 1, 5))
print("Replace:", string.gsub(combined, "World", "Lua"))
-- String to number conversion
local numStr = "123"
local strNum = 456
print("String→Number:", tonumber(numStr), type(tonumber(numStr)))
print("Number→String:", tostring(strNum), type(tostring(strNum)))
-- Escape sequences
local escaped = "Newline\n Tab\t Quote\" Backslash\\"
print("Escaped string:")
print(escaped)
-- Long string literals
local longString = [[
Multi-line
long string
can be written.
Escape characters are ignored: \n \t \"
]]
print("Long string:", longString)
-- Tables (Lua's most important data structure)
print("\n=== Tables ===")
-- Used as arrays
local array = {10, 20, 30, 40, 50}
print("Array:")
for i = 1, #array do -- Lua arrays start from 1
print(" [" .. i .. "] = " .. array[i])
end
-- Used as dictionary (hash table)
local dict = {
name = "John Doe",
age = 25,
city = "New York",
isActive = true
}
print("\nDictionary:")
for key, value in pairs(dict) do
print(" " .. key .. " = " .. tostring(value))
end
-- Mixed table (array and dictionary features simultaneously)
local mixed = {
"First element", -- [1]
"Second element", -- [2]
name = "Mixed table",
count = 2
}
print("\nMixed table:")
print(" [1] =", mixed[1])
print(" [2] =", mixed[2])
print(" name =", mixed.name)
print(" count =", mixed.count)
-- Nested tables
local nested = {
personal = {
name = "Jane Smith",
age = 30
},
work = {
company = "Sample Company",
position = "Engineer"
},
hobbies = {"reading", "movies", "programming"}
}
print("\nNested table:")
print(" Name:", nested.personal.name)
print(" Age:", nested.personal.age)
print(" Company:", nested.work.company)
print(" Hobbies:")
for i, hobby in ipairs(nested.hobbies) do
print(" " .. i .. ". " .. hobby)
end
-- Dynamic table manipulation
local dynamic = {}
dynamic.name = "Dynamic table"
dynamic[1] = "First element"
dynamic["key with spaces"] = "Key with spaces"
-- table.insert and table.remove
table.insert(dynamic, "Added element")
table.insert(dynamic, 2, "Inserted at position 2")
print("\nDynamic table:")
for k, v in pairs(dynamic) do
print(" " .. tostring(k) .. " = " .. tostring(v))
end
-- Variable scope
globalVar = "Global variable" -- No 'local'
do
local localVar = "Local variable"
print("\nScope test:")
print(" Inside block - Global:", globalVar)
print(" Inside block - Local:", localVar)
end
print("Outside block - Global:", globalVar)
-- print("Outside block - Local:", localVar) -- Would cause error
-- Multiple assignment
local a, b, c = 1, 2, 3
local x, y = "Hello", "World"
print("\nMultiple assignment:")
print("a, b, c =", a, b, c)
print("x, y =", x, y)
-- Value swapping
a, b = b, a
print("After swap: a, b =", a, b)
-- Multiple return values from function
local function getNameAge()
return "Bob Johnson", 35
end
local name, age = getNameAge()
print("Multiple return values:", name, age)
-- Weak reference tables (garbage collection related)
local weak = {}
setmetatable(weak, {__mode = "v"}) -- Make values weak references
weak[1] = {data = "test data"}
print("\nWeak reference table created")
Functions and Control Structures
-- Basic function definition
function add(a, b)
return a + b
end
-- Local function
local function multiply(a, b)
return a * b
end
-- Anonymous function (function literal)
local subtract = function(a, b)
return a - b
end
print("=== Basic Functions ===")
print("Addition:", add(5, 3))
print("Multiplication:", multiply(4, 6))
print("Subtraction:", subtract(10, 3))
-- Variadic function
local function sum(...)
local args = {...} -- Convert varargs to table
local total = 0
for i = 1, #args do
total = total + args[i]
end
return total
end
print("Variadic sum:", sum(1, 2, 3, 4, 5))
-- Variadic argument processing using select
local function printArgs(...)
print("Number of arguments:", select("#", ...))
for i = 1, select("#", ...) do
print(" Arg" .. i .. ":", select(i, ...))
end
end
print("\nVariadic test:")
printArgs("Hello", "World", 123, true)
-- Multiple return values
local function getMinMax(...)
local args = {...}
if #args == 0 then return nil, nil end
local min, max = args[1], args[1]
for i = 2, #args do
if args[i] < min then min = args[i] end
if args[i] > max then max = args[i] end
end
return min, max
end
local minimum, maximum = getMinMax(5, 2, 8, 1, 9, 3)
print("\nMin・Max:", minimum, maximum)
-- Closures (functions within functions)
local function createCounter(initial)
local count = initial or 0
return function()
count = count + 1
return count
end
end
local counter1 = createCounter(0)
local counter2 = createCounter(100)
print("\nClosure test:")
print("Counter1:", counter1(), counter1(), counter1()) -- 1, 2, 3
print("Counter2:", counter2(), counter2()) -- 101, 102
-- Higher-order functions (functions that take functions as arguments)
local function applyToArray(arr, func)
local result = {}
for i, v in ipairs(arr) do
result[i] = func(v)
end
return result
end
local numbers = {1, 2, 3, 4, 5}
local doubled = applyToArray(numbers, function(x) return x * 2 end)
local squared = applyToArray(numbers, function(x) return x * x end)
print("\nHigher-order functions:")
print("Original array:", table.concat(numbers, ", "))
print("Doubled:", table.concat(doubled, ", "))
print("Squared:", table.concat(squared, ", "))
-- Conditional statements
print("\n=== Conditional Statements ===")
local score = 85
if score >= 90 then
print("Excellent")
elseif score >= 80 then
print("Good")
elseif score >= 70 then
print("Average")
elseif score >= 60 then
print("Needs Improvement")
else
print("Failed")
end
-- Alternative to ternary operator (using and/or operators)
local grade = score >= 60 and "Pass" or "Fail"
print("Result:", grade)
-- Loop structures
print("\n=== Loop Structures ===")
-- Numeric for loop
print("Numeric for loop:")
for i = 1, 5 do
print(" Count:", i)
end
print("For loop with step:")
for i = 10, 2, -2 do -- From 10 to 2 with step -2
print(" Reverse:", i)
end
-- Generic for loop (iterator)
local fruits = {"apple", "banana", "orange", "strawberry"}
print("Using ipairs (with index):")
for index, fruit in ipairs(fruits) do
print(" " .. index .. ": " .. fruit)
end
local person = {name = "Smith", age = 30, city = "London"}
print("Using pairs (key-value):")
for key, value in pairs(person) do
print(" " .. key .. " = " .. tostring(value))
end
-- While loop
print("While loop:")
local count = 1
while count <= 3 do
print(" while:", count)
count = count + 1
end
-- Repeat loop (equivalent to do-while)
print("Repeat loop:")
count = 1
repeat
print(" repeat:", count)
count = count + 1
until count > 3
-- Break and control structures
print("Using break statement:")
for i = 1, 10 do
if i == 5 then
print(" Break at 5")
break
end
print(" Value:", i)
end
-- Goto statement (Lua 5.2+)
print("Goto example:")
local i = 1
::loop::
if i <= 3 then
print(" goto:", i)
i = i + 1
goto loop
end
-- Error handling
print("\n=== Error Handling ===")
-- Safe function call using pcall (protected call)
local function riskyFunction(x)
if x == 0 then
error("Zero is not allowed")
end
return 10 / x
end
local success, result = pcall(riskyFunction, 5)
if success then
print("Success:", result)
else
print("Error:", result)
end
local success, result = pcall(riskyFunction, 0)
if success then
print("Success:", result)
else
print("Error:", result)
end
-- xpcall (extended pcall) with error handler
local function errorHandler(err)
return "Handled by error handler: " .. tostring(err)
end
local success, result = xpcall(function() return riskyFunction(0) end, errorHandler)
print("xpcall result:", success, result)
-- Assert function
local function validateAge(age)
assert(type(age) == "number", "Age must be a number")
assert(age >= 0 and age <= 150, "Age must be between 0 and 150")
return age
end
local validAge = validateAge(25)
print("Valid age:", validAge)
-- Invalid age test (commented out)
-- local invalidAge = validateAge("abc") -- Would cause error
Tables and Metatables
-- One of Lua's most powerful features: Metatables
print("=== Advanced Table Operations ===")
-- Basic table operations
local array = {10, 20, 30}
print("Original array:", table.concat(array, ", "))
-- Element addition
table.insert(array, 40) -- Add to end
table.insert(array, 2, 15) -- Insert at position 2
print("After addition:", table.concat(array, ", "))
-- Element removal
local removed = table.remove(array, 2) -- Remove 2nd element
print("Removed element:", removed)
print("After removal:", table.concat(array, ", "))
-- Sorting
local unsorted = {3, 1, 4, 1, 5, 9, 2, 6}
table.sort(unsorted)
print("After sort:", table.concat(unsorted, ", "))
-- Custom sorting
local names = {"Smith", "Johnson", "Brown", "Davis"}
table.sort(names, function(a, b) return #a < #b end) -- Sort by length
print("Sort by length:", table.concat(names, ", "))
-- Metatable basics
print("\n=== Metatables ===")
-- Simple metatable example
local mt = {
__add = function(a, b)
return {x = a.x + b.x, y = a.y + b.y}
end,
__tostring = function(t)
return string.format("Point(%d, %d)", t.x, t.y)
end,
__index = function(t, key)
if key == "magnitude" then
return math.sqrt(t.x^2 + t.y^2)
end
return nil
end
}
local point1 = {x = 3, y = 4}
local point2 = {x = 1, y = 2}
setmetatable(point1, mt)
setmetatable(point2, mt)
-- Using metamethods
local point3 = point1 + point2 -- __add metamethod
print("Point addition:", tostring(point3)) -- __tostring metamethod
print("point1 magnitude:", point1.magnitude) -- __index metamethod
-- Class-like objects
print("\n=== Object-Oriented Programming ===")
-- Person class definition
local Person = {}
Person.__index = Person
function Person:new(name, age)
local obj = {
name = name or "Unknown",
age = age or 0
}
setmetatable(obj, self)
return obj
end
function Person:introduce()
return string.format("My name is %s and I am %d years old.", self.name, self.age)
end
function Person:haveBirthday()
self.age = self.age + 1
print(self.name .. ", happy " .. self.age .. "th birthday!")
end
function Person:__tostring()
return string.format("Person{name='%s', age=%d}", self.name, self.age)
end
-- Object creation and usage
local person1 = Person:new("John Doe", 25)
local person2 = Person:new("Jane Smith", 30)
print("person1:", person1:introduce())
print("person2:", person2:introduce())
person1:haveBirthday()
print("After birthday:", tostring(person1))
-- Inheritance example
local Student = {}
Student.__index = Student
setmetatable(Student, Person) -- Inheritance from Person to Student
function Student:new(name, age, school)
local obj = Person.new(self, name, age)
obj.school = school or "Unspecified"
setmetatable(obj, self)
return obj
end
function Student:introduce()
local base = Person.introduce(self)
return base .. " I study at " .. self.school .. "."
end
function Student:study(subject)
print(self.name .. " is studying " .. subject .. ".")
end
local student = Student:new("Bob Johnson", 20, "MIT")
print("Student introduction:", student:introduce())
student:study("Mathematics")
-- Proxy table (controlling property access)
print("\n=== Proxy Tables ===")
local function createReadOnlyTable(t)
local proxy = {}
local mt = {
__index = t,
__newindex = function(table, key, value)
error("Writing to read-only table is forbidden")
end
}
setmetatable(proxy, mt)
return proxy
end
local config = {
version = "1.0",
author = "Developer",
debug = true
}
local readOnlyConfig = createReadOnlyTable(config)
print("Version:", readOnlyConfig.version)
print("Author:", readOnlyConfig.author)
-- Write test (would cause error)
-- readOnlyConfig.version = "2.0" -- Error
-- Property access monitoring
local function createWatchedTable()
local data = {}
local watchers = {}
local mt = {
__index = data,
__newindex = function(t, key, value)
local oldValue = data[key]
data[key] = value
-- Notify watchers
for _, watcher in ipairs(watchers) do
watcher(key, oldValue, value)
end
end
}
local proxy = {}
setmetatable(proxy, mt)
-- Add watcher method
proxy.addWatcher = function(watcher)
table.insert(watchers, watcher)
end
return proxy
end
local watched = createWatchedTable()
watched.addWatcher(function(key, oldVal, newVal)
print(string.format("Property change: %s = %s → %s",
key, tostring(oldVal), tostring(newVal)))
end)
watched.name = "Test"
watched.count = 1
watched.count = 2
Modules and Packages
-- Module system in Lua
print("=== Module Basics ===")
-- Simple module creation example
local mymath = {}
function mymath.add(a, b)
return a + b
end
function mymath.multiply(a, b)
return a * b
end
mymath.pi = 3.14159
-- Private function (not accessible from outside module)
local function privateHelper(x)
return x * 2
end
function mymath.doubleAndAdd(a, b)
return privateHelper(a) + b
end
-- Module usage
print("mymath.add(5, 3):", mymath.add(5, 3))
print("mymath.multiply(4, 6):", mymath.multiply(4, 6))
print("mymath.pi:", mymath.pi)
print("mymath.doubleAndAdd(5, 3):", mymath.doubleAndAdd(5, 3))
-- Advanced module patterns
print("\n=== Advanced Module Patterns ===")
-- Module with namespaces
local utils = {}
utils.string = {}
utils.table = {}
utils.math = {}
-- String utilities
function utils.string.trim(s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
function utils.string.split(str, delimiter)
local result = {}
local pattern = string.format("([^%s]+)", delimiter)
for match in str:gmatch(pattern) do
table.insert(result, match)
end
return result
end
-- Table utilities
function utils.table.deepCopy(orig)
local copy
if type(orig) == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[utils.table.deepCopy(orig_key)] = utils.table.deepCopy(orig_value)
end
setmetatable(copy, utils.table.deepCopy(getmetatable(orig)))
else
copy = orig
end
return copy
end
function utils.table.isEmpty(t)
return next(t) == nil
end
-- Math utilities
function utils.math.clamp(value, min, max)
return math.max(min, math.min(max, value))
end
function utils.math.lerp(a, b, t)
return a + (b - a) * t
end
-- Utility usage examples
print("String trim:", "'" .. utils.string.trim(" hello world ") .. "'")
local parts = utils.string.split("apple,banana,orange", ",")
print("String split:")
for i, part in ipairs(parts) do
print(" " .. i .. ": " .. part)
end
local original = {a = 1, b = {c = 2, d = 3}}
local copied = utils.table.deepCopy(original)
copied.b.c = 999
print("Original table b.c:", original.b.c) -- 2 (unchanged)
print("Copied table b.c:", copied.b.c) -- 999
print("clamp(15, 0, 10):", utils.math.clamp(15, 0, 10))
print("lerp(0, 100, 0.5):", utils.math.lerp(0, 100, 0.5))
-- Factory pattern
print("\n=== Factory Pattern ===")
local function createLogger(name)
local logger = {}
function logger.info(message)
print(string.format("[INFO][%s] %s", name, message))
end
function logger.warn(message)
print(string.format("[WARN][%s] %s", name, message))
end
function logger.error(message)
print(string.format("[ERROR][%s] %s", name, message))
end
return logger
end
local appLogger = createLogger("App")
local dbLogger = createLogger("Database")
appLogger.info("Application started")
dbLogger.warn("Connection is slow")
appLogger.error("Unexpected error")
-- Singleton pattern
print("\n=== Singleton Pattern ===")
local ConfigManager = {}
local instance = nil
function ConfigManager.getInstance()
if not instance then
instance = {
settings = {},
set = function(self, key, value)
self.settings[key] = value
end,
get = function(self, key, default)
return self.settings[key] or default
end,
dump = function(self)
print("Configuration:")
for k, v in pairs(self.settings) do
print(" " .. k .. " = " .. tostring(v))
end
end
}
end
return instance
end
-- Singleton usage
local config1 = ConfigManager.getInstance()
local config2 = ConfigManager.getInstance()
config1:set("debug", true)
config2:set("version", "1.0")
print("config1 and config2 same instance?", config1 == config2)
config1:dump()
-- Package loader simulation
print("\n=== Package System Simulation ===")
local packageSystem = {}
packageSystem.loaded = {}
function packageSystem.require(name)
if packageSystem.loaded[name] then
return packageSystem.loaded[name]
end
-- In actual system, would load from files
local module
if name == "json" then
module = {
encode = function(t) return "JSON encoded: " .. tostring(t) end,
decode = function(s) return {decoded = s} end
}
elseif name == "http" then
module = {
get = function(url) return "HTTP GET: " .. url end,
post = function(url, data) return "HTTP POST to " .. url end
}
else
error("Module '" .. name .. "' not found")
end
packageSystem.loaded[name] = module
return module
end
-- Package usage
local json = packageSystem.require("json")
local http = packageSystem.require("http")
print("JSON encode:", json.encode({test = "data"}))
print("HTTP GET:", http.get("http://example.com"))
-- Second require returns already loaded module
local json2 = packageSystem.require("json")
print("Same instance?", json == json2)
File Operations and C Integration
-- File input/output operations
print("=== File Input/Output ===")
-- Writing to file
local function writeToFile()
local filename = "test.txt"
local file = io.open(filename, "w")
if file then
file:write("Hello, Lua!\n")
file:write("This is a test file.\n")
file:write("Current time: " .. os.date("%Y-%m-%d %H:%M:%S") .. "\n")
-- Writing numeric data
for i = 1, 5 do
file:write(string.format("Number %d: %d\n", i, i * i))
end
file:close()
print("File write completed: " .. filename)
return true
else
print("Failed to create file")
return false
end
end
-- Reading from file
local function readFromFile()
local filename = "test.txt"
local file = io.open(filename, "r")
if file then
print("\nFile content:")
print("=" .. string.rep("=", 30))
-- Read entire content at once
local content = file:read("*all")
file:close()
print(content)
print("=" .. string.rep("=", 30))
return true
else
print("Failed to read file")
return false
end
end
-- Line-by-line reading
local function readLineByLine()
local filename = "test.txt"
local file = io.open(filename, "r")
if file then
print("\nLine-by-line reading:")
local lineNumber = 1
for line in file:lines() do
print(string.format("Line%d: %s", lineNumber, line))
lineNumber = lineNumber + 1
end
file:close()
return true
else
print("Failed to read file")
return false
end
end
-- Binary file operations
local function binaryFileOperations()
local filename = "binary.dat"
-- Binary write
local file = io.open(filename, "wb")
if file then
-- Write some byte values
for i = 0, 255, 17 do
file:write(string.char(i))
end
file:close()
print("Binary file created: " .. filename)
end
-- Binary read
file = io.open(filename, "rb")
if file then
print("Binary file content:")
local byte = file:read(1)
local position = 1
while byte do
print(string.format("Position%d: %d (0x%02X)", position, string.byte(byte), string.byte(byte)))
byte = file:read(1)
position = position + 1
end
file:close()
end
end
-- CSV file operations
local function csvOperations()
local csvFile = "data.csv"
-- CSV file creation
local file = io.open(csvFile, "w")
if file then
file:write("Name,Age,Department,Salary\n")
file:write("John Doe,25,Sales,300000\n")
file:write("Jane Smith,30,Development,400000\n")
file:write("Bob Johnson,35,Management,350000\n")
file:close()
print("CSV file created: " .. csvFile)
end
-- CSV reading and parsing
file = io.open(csvFile, "r")
if file then
print("\nCSV data:")
local header = file:read("*line")
print("Header: " .. header)
local rowNumber = 1
for line in file:lines() do
local fields = {}
for field in line:gmatch("([^,]+)") do
table.insert(fields, field)
end
print(string.format("Row%d: Name=%s, Age=%s, Dept=%s, Salary=%s",
rowNumber, fields[1], fields[2], fields[3], fields[4]))
rowNumber = rowNumber + 1
end
file:close()
end
end
-- Execute file operations
if writeToFile() then
readFromFile()
readLineByLine()
end
binaryFileOperations()
csvOperations()
-- System information and OS operations
print("\n=== System Information ===")
print("OS time:", os.date("%Y-%m-%d %H:%M:%S"))
print("UTC time:", os.date("!%Y-%m-%d %H:%M:%S"))
print("Epoch time:", os.time())
-- Date calculation
local birthday = os.time{year=1990, month=5, day=15}
local now = os.time()
local ageInSeconds = now - birthday
local ageInDays = math.floor(ageInSeconds / (24 * 60 * 60))
print("Days since birthday:", ageInDays .. " days")
-- Environment variables (if they exist)
local home = os.getenv("HOME") or os.getenv("USERPROFILE")
if home then
print("Home directory:", home)
end
local path = os.getenv("PATH")
if path then
print("PATH environment variable length:", #path .. " characters")
end
-- Using C library functions
print("\n=== C Library Functions ===")
-- Math library
print("Math functions:")
print("sin(π/2):", math.sin(math.pi / 2))
print("cos(0):", math.cos(0))
print("sqrt(16):", math.sqrt(16))
print("log(math.e):", math.log(math.e))
print("random(1,10):", math.random(1, 10))
-- Random seed setting
math.randomseed(os.time())
print("Random array:")
local randomNumbers = {}
for i = 1, 5 do
randomNumbers[i] = math.random(1, 100)
end
print(" " .. table.concat(randomNumbers, ", "))
-- String library
print("\nString functions:")
local testString = "Hello, Lua Programming!"
print("Original string:", testString)
print("Uppercase:", string.upper(testString))
print("Lowercase:", string.lower(testString))
print("String length:", string.len(testString))
print("Substring(8-10):", string.sub(testString, 8, 10))
-- Pattern matching
local email = "[email protected]"
local username, domain = string.match(email, "([^@]+)@([^@]+)")
print("Email parsing:")
print(" Username:", username)
print(" Domain:", domain)
-- String replacement
local text = "Today is sunny. Tomorrow will be sunny too."
local replaced = string.gsub(text, "sunny", "rainy")
print("Before replacement:", text)
print("After replacement:", replaced)
-- Table library
print("\nTable functions:")
local fruits = {"apple", "banana", "orange", "strawberry"}
print("Original array:", table.concat(fruits, ", "))
table.sort(fruits)
print("After sort:", table.concat(fruits, ", "))
local numbers = {3, 1, 4, 1, 5, 9, 2, 6}
table.sort(numbers, function(a, b) return a > b end) -- Descending
print("Descending sort:", table.concat(numbers, ", "))
-- Simple C integration simulation (actual implementation requires C-side code)
print("\n=== C Integration Concepts ===")
print("In actual C integration, you can define functions like:")
print("- Implement high-speed computation functions in C")
print("- Call C functions from Lua")
print("- Execute Lua scripts from C")
print("- Provide C functions as shared libraries")
-- Example of using C extensions to complement Lua's limitations
local function simulateCExtension()
print("\nTypical uses for C extensions:")
print("1. Accelerating numerical computations")
print("2. Accessing system APIs")
print("3. Leveraging existing C libraries")
print("4. Memory-intensive processing")
print("5. Real-time processing")
end
simulateCExtension()
Advantages and Disadvantages
Advantages
- Lightweight: Very small memory footprint (few dozen KB)
- High Performance: Fast execution despite compact size
- Easy Embedding: Simple integration into C/C++ applications
- Simple Syntax: Easy to learn and readable code
- Flexible Data Structures: Unified arrays/dictionaries/objects through tables
- Metaprogramming: Powerful feature extension through metatables
Disadvantages
- Limited Ecosystem: Minimal standard library
- Small Community: Smaller compared to other major languages
- Lack of Debug Tools: Limited development tools and IDE support
- 1-based Arrays: Different array indexing from most languages
- Global Variable Issues: Default global scope can be problematic
- Error Messages: Limited detailed information for runtime errors
Reference Links
Official Documentation
- Lua Official Site - Lua official website
- Lua Reference Manual - Official reference manual
- Programming in Lua - Official programming guide
Learning Resources
- Learn Lua in Y Minutes - Quick start guide
- Lua Tutorial - Basic tutorial
- Lua Users Wiki - Community wiki
Development Tools & Libraries
- LuaRocks - Lua package manager
- LÖVE 2D - Game development framework for Lua
- OpenResty - Lua utilization with Nginx
- Wireshark Lua - Wireshark protocol analysis
Use Cases & Frameworks
- Redis Lua Scripting - Script execution in Redis
- World of Warcraft Addons - WoW addon development
- Kong API Gateway - Lua plugins
- Roblox Lua - Game development platform