1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
print("=== Lua Standard Library Example ===")
print("\n1. string Library (String Operations)")
local str = "Hello, Lua Programming!" print("Original string:", str)
print("Length:", string.len(str)) print("Length (using #):", #str)
print("Uppercase:", string.upper(str)) print("Lowercase:", string.lower(str))
print("Substring (1-5):", string.sub(str, 1, 5)) print("Substring (8-end):", string.sub(str, 8)) print("Substring (from end):", string.sub(str, -11))
local start_pos, end_pos = string.find(str, "Lua") print("Find 'Lua':", start_pos, end_pos)
local new_str = string.gsub(str, "Lua", "LUA") print("Replace 'Lua' with 'LUA':", new_str)
local formatted = string.format("%s is %d years old and has %.2f GPA", "Alice", 25, 3.85) print("Formatted string:", formatted)
print("Repeat 'Hi ' 3 times:", string.rep("Hi ", 3))
local email = "user@example.com" local username = string.match(email, "(%a+)@") local domain = string.match(email, "@(%a+%.%a+)") print("Email parts - username:", username, "domain:", domain)
local csv = "apple,banana,orange,grape" local fruits = {} for fruit in string.gmatch(csv, "[^,]+") do table.insert(fruits, fruit) end print("Split CSV:", table.concat(fruits, " | "))
print("\n2. math Library (Mathematical Functions)")
print("math.abs(-10):", math.abs(-10)) print("math.floor(3.7):", math.floor(3.7)) print("math.ceil(3.2):", math.ceil(3.2)) print("math.sqrt(16):", math.sqrt(16))
print("math.sin(math.pi/2):", math.sin(math.pi/2)) print("math.cos(math.pi):", math.cos(math.pi)) print("math.tan(math.pi/4):", math.tan(math.pi/4)) print("math.asin(1):", math.asin(1))
print("math.exp(1):", math.exp(1)) print("math.log(math.exp(1)):", math.log(math.exp(1))) print("math.log10(100):", math.log10(100)) print("math.pow(2, 10):", math.pow(2, 10))
print("math.max(10, 5, 20, 15):", math.max(10, 5, 20, 15)) print("math.min(10, 5, 20, 15):", math.min(10, 5, 20, 15))
math.randomseed(os.time()) print("Random (0-1):", math.random()) print("Random (1-10):", math.random(10)) print("Random (5-15):", math.random(5, 15))
print("math.pi:", math.pi) print("math.huge:", math.huge)
print("\n3. os Library (Operating System Interaction)")
print("os.time():", os.time()) print("os.date():", os.date()) print("os.date('%Y-%m-%d'):", os.date("%Y-%m-%d")) print("os.date('%H:%M:%S'):", os.date("%H:%M:%S"))
print("\nExecute system command 'echo Hello':") local handle = io.popen("echo Hello") if handle then local result = handle:read("*a") print("Result:", result) handle:close() end
print("\nos.getenv('PATH'):", os.getenv("PATH"))
print("os.clock():", os.clock()) print("os.difftime(os.time(), os.time() - 3600):", os.difftime(os.time(), os.time() - 3600))
print("\n4. io Library (File Operations)")
print("\nStandard I/O:")
local tmp = io.tmpfile() tmp:write("Temporary data\n") tmp:seek("set") print("Temporary file content:", tmp:read("*l")) tmp:close()
print("\n5. table Library (Table Operations)")
local colors = {"red", "green", "blue"} print("table.concat(colors, ', '):", table.concat(colors, ", "))
local numbers = {1, 2, 3, 4, 5} table.insert(numbers, 3, 100) print("After table.insert(numbers, 3, 100):", table.concat(numbers, ", "))
local removed = table.remove(numbers, 3) print("After table.remove(numbers, 3):", table.concat(numbers, ", ")) print("Removed element:", removed)
local unsorted = {5, 2, 8, 1, 9, 3} table.sort(unsorted) print("table.sort(unsorted):", table.concat(unsorted, ", "))
local coords = {10, 20, 30} local x, y, z = unpack(coords) print("unpack(coords):", x, y, z)
local mixed = {"a", "b", name = "mixed", value = 100, "c"} print("\nUsing ipairs (array part only):") for i, v in ipairs(mixed) do print(i, v) end
print("\nUsing pairs (all elements):") for k, v in pairs(mixed) do print(k, v) end
print("\n6. debug Library (Debugging Functions)")
function test_debug() local info = debug.getinfo(1) print("Current function name:", info.name) print("Line number:", info.currentline) end test_debug()
print("\ndebug.getmetatable(_G):", debug.getmetatable(_G))
print("\n7. coroutine Library (Coroutines)")
local co = coroutine.create(function() for i = 1, 3 do print("Coroutine yielding:", i) coroutine.yield(i) end return "Coroutine finished" end)
print("Coroutine status:", coroutine.status(co))
local status, result = coroutine.resume(co) print("Resume 1 - Status:", status, "Result:", result) print("Coroutine status:", coroutine.status(co))
status, result = coroutine.resume(co) print("Resume 2 - Status:", status, "Result:", result)
status, result = coroutine.resume(co) print("Resume 3 - Status:", status, "Result:", result)
status, result = coroutine.resume(co) print("Resume 4 - Status:", status, "Result:", result) print("Coroutine status:", coroutine.status(co))
print("\n8. package Library (Module Management)")
print("package.path:", package.path) print("package.cpath:", package.cpath) print("package.loaded:", next(package.loaded))
print("\n9. Utility Functions")
function split(str, sep) local result = {} for part in string.gmatch(str, "[^" .. sep .. "]+") do table.insert(result, part) end return result end
local csv_str = "apple,banana,orange,grape" local parts = split(csv_str, ",") print("Split CSV:", table.concat(parts, " | "))
function deepcopy(orig) local orig_type = type(orig) local copy if orig_type == 'table' then copy = {} for orig_key, orig_value in next, orig, nil do copy[deepcopy(orig_key)] = deepcopy(orig_value) end setmetatable(copy, deepcopy(getmetatable(orig))) else copy = orig end return copy end
local original = {a = 1, b = {c = 2}} local copied = deepcopy(original) copied.b.c = 100 print("Original.b.c:", original.b.c) print("Copied.b.c:", copied.b.c)
function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end
local mixed_table = {"a", "b", name = "test", value = 42} print("#mixed_table:", #mixed_table) print("tablelength(mixed_table):", tablelength(mixed_table))
print("\n=== End of Standard Library Examples ===")
|