local name = "Oak Lua"
local version = 5.4
local is_active = true
local data = nil
local count = 42
local pi = 3.14159
local hex_value = 0xFF
local scientific = 1.23e-4
local x = 10
local y = 20
function greet(name)
return "Hello, " .. name .. "!"
end
local function add(a, b)
return a + b
end
local multiply = function(x, y)
return x * y
end
if count > 0 then
print("Count is positive")
elseif count < 0 then
print("Count is negative")
else
print("Count is zero")
end
for i = 1, 10 do
print("Iteration: " .. i)
end
local i = 1
while i <= 5 do
print("While loop: " .. i)
i = i + 1
end
repeat
print("Repeat-until loop")
i = i - 1
until i <= 0
local person = {
name = "John Doe",
age = 30,
city = "New York",
hobbies = {"reading", "gaming", "hiking"}
}
local array = {10, 20, 30, 40, 50}
local mixed = {1, "two", 3.0, true, nil, {nested = "table"}}
local matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
local message = "Hello World"
local upper = string.upper(message)
local lower = string.lower(message)
local length = #message
local substring = string.sub(message, 1, 5)
local sum = 10 + 20
local difference = 30 - 15
local product = 5 * 6
local quotient = 20 / 4
local modulo = 17 % 5
local power = 2 ^ 8
local equal = (5 == 5)
local not_equal = (5 ~= 6)
local less_than = (3 < 7)
local greater_than = (8 > 2)
local less_equal = (4 <= 4)
local greater_equal = (9 >= 9)
local and_result = true and false
local or_result = true or false
local not_result = not true
local first_name = "John"
local last_name = "Doe"
local full_name = first_name .. " " .. last_name
local global_var = "I'm global"
do
local local_var = "I'm local"
print(local_var) end
local mt = {
__add = function(a, b)
return a.value + b.value
end,
__tostring = function(t)
return "CustomObject: " .. tostring(t.value)
end
}
local obj1 = {value = 10}
local obj2 = {value = 20}
setmetatable(obj1, mt)
setmetatable(obj2, mt)
local result = obj1 + obj2
print(result)
local success, error_msg = pcall(function()
error("This is an intentional error")
end)
if not success then
print("Error caught: " .. error_msg)
end
local co = coroutine.create(function(a, b)
print("Coroutine started with:", a, b)
local yield_value = coroutine.yield(a + b)
print("Coroutine resumed with:", yield_value)
return a * b
end)
print("Coroutine status:", coroutine.status(co))
local ok, result = coroutine.resume(co, 10, 20)
print("First resume result:", result)
local M = {}
function M.add(a, b)
return a + b
end
function M.multiply(a, b)
return a * b
end
return M
local unpack = table.unpack or unpack
local function varargs(...)
local args = {...}
print("Received", #args, "arguments")
for i, arg in ipairs(args) do
print(i, arg)
end
end
varargs(1, 2, 3, "four", 5.0)
local function factorial(n, acc)
acc = acc or 1
if n <= 1 then
return acc
end
return factorial(n - 1, n * acc) end
print("5! =", factorial(5))