兩種用法數組
print("Hello, World") a = math.sin(3) + math.cos(10) print(os.date())
print "Hello, World" -- 等價於 print("Hello, World") print {1, 2, 3} -- 等價於 print({1, 2, 3}) function add(a) local sum = 0 for i, v in ipairs(a) do sum = sum + v end return sum end b = {1, 2, 3} add(b)
function f(a, b) return a or b end f(3) -- a = 3, b = nil f(3, 4) -- a = 3, b = 4 f(3, 4, 5) -- a = 3, b = 4 ,5 被丟棄了
定義一個全局的計數器函數
function intCount(n) n = n or 1 -- 賦值一個默認值 count = count + 1 end
面向對象式調用函數lua
-- 用於在字符串中定位一個模式的函數 string.find print(string.find("Hello Lua users", "Lua")) -- 開始的位置 7, 結束的位置 9
-- 查找數組中的最大元素,並返回這個元素的所在位置 function maximum(a) local mi = 1 -- 最大值的索引 local max = a[mi] -- 最大值 for i,v in ipairs(a) do if v > max then mi = i max = v end end return max, mi end maximum(a) -- 沒有任何反應 print(maximum({3, 4, 23, 5, 7}) -- 23 3 print(maximum({3, 4, 23, 5, 7} .. "a") -- 23a
一系列表達式在 Lua 中的 4 中狀況調試
多重賦值code
function foo0() end function foo1() return "a" end function foo2() return "a", "b" end -- 多重賦值的4種狀況 -- 第一種狀況 x, y = foo2() -- x = "a" , y = "b" x = foo2() -- x = "a" x, y, z = 10, foo2() -- x = 10, y = "a", z = "b" -- 第二種狀況 x, y = foo0() -- x = nil, y = nil x, y = foo1() -- x = "a", y = nil x, y, z = foo2() -- x = "a", y = "b", z = nil -- 第三種狀況 x, y = foo2(), 20 -- x = "a", y = 20 x, y = foo0(), 20, 30 -- x = nil, y = 20 -- 第四種狀況 print(foo0()) -- 不會打印任何值 print(foo1()) -- a print(foo2()) -- a, b print(foo2(), 20) -- a, 1 print(foo2() .. "x") -- ax
table 構造式orm
function foo0() end function foo1() return "a" end function foo2() return "a", "b" end t = {foo2()} -- t = {"a", "b"} t = {foo2(), "x"} -- t = {"a", "x"}
return對象
function foo0() end function foo1() return "a" end function foo2() return "a", "b" end function foo(i) if i == 0 then return foo0() elseif i == 1 then return foo1() elseif i == 2 then return foo2() end end print(foo(1)) -- a print(foo(2)) -- a, b print(foo(0)) -- 無返回值 -- () 包裹 print((foo(1)) -- a print((foo(2)) -- a print((foo(0)) -- nil 不太懂爲何
unpack 函數遞歸
print(unpack{10, 20, 30}) -- 10 20 30 a, b = unpack{10, 20, 30} -- a = 10, b = 20
-- 調用任意函數 f, 而全部的參數都在數組 a 中 -- unpack 將返回 a 中的全部值,這些值做爲 f 的實參 f(unpack(a)) f = string.find a = {"hello", "ll"} f(unpack(a)) -- 3 4 等效於 string.find("hello", "ll")
用 lua 遞歸實現 unpack索引
function unpack(t, i) i = i or 1 if t[i] then return t[i], unpack(t, i + 1) end end
-- 返回全部參數的和 function add(...) local s = 0 for i, v in ipairs{...} do -- 表達式{...}表示一個由變長參數構成的數組 s = s + v end return s end print(add(3, 4, 5, 100)) -- 115 -- 調試技巧 ,相似與直接調用函數 foo ,但在調用 foo 前先調用 print 打印其全部的實參 function foo1(...) print("calling foo:", ...) return foo(...) end -- 獲取函數的實參列表 function foo(a, b, c) end function foo(...) local a, b, c = ... end -- 格式化文本 string.format ,輸出文本 io.write -- 固定參數必定要在變長參數以前 function fwrite(fmt, ...) return io.write(string.format(fmt, ...)) end fwrite() -- fmt = nil fwrite("a") -- fmt = a fwrite("%d%d", 4, 5) -- fmt = "%d%d" , 變長參數 = 4, 5 for i = 1, select('#', ...) do local arg = select('#', ...) <循環體> end
os.rename -- 文件更名,但願達到的效果 os.rename(old = "temp.lua", new = "temp1.lua") -- lua 不支持註釋的寫法 rename = {old = "temp.lua", new = "temp1.lua"} function rename (arg) return os.rename(arg.old, arg.new) end x = Window{x = 0, y = 0, width = 300, height = 200, title = "Lua", background = "blue", border = "true"} -- Window 函數根據要求檢查必填參數,或爲某些函數添加默認值 -- 假設 _Window 是真正用於建立新窗口的函數,要求全部參數以正確次序傳入 function Window(options) if type(options.title) ~= "string" then error("no title") elseif type(options.width) ~= "number" then error("no width") elseif type(options.height) ~= "height" then error("no height") end _Window(options.title, options.x or 0 -- 默認值 options.y or 0 -- 默認值 options.width, options.height, options.background or "white" -- 默認值 options.border -- 默認值爲 false(nil) )