a = a * 2 b = a * b a = a * 2; b = a * b a = a * b; b = a * b a = a * b b = a * b
在交互模式中輸入的一行內容會被解釋器看成一個完整的程序塊,若是這一行的內容不足以構成一個完整的程序塊,就會等待輸入html
Ctrl + Z
是 end-of-file 控制字符,在 dos 中是這個快捷鍵os.exit()
標準庫中的退出函數dofile("文件路徑 / 須要轉義")
加載函數庫-- 階乘函數 function fact(n) if n == 0 then return 1 --0 的階乘是 1 else return n * fact(n - 1) -- 3 的階乘, 3 * 2 * 1 end end print("Enter a number:") a = io.read("*number") -- 讀取用戶輸入且需爲數字類型的 print(fact(a)) --調用階乘函數,並傳入實參 a -- lib1 函數庫 function norm(x, y) return (x ^ 2 + y ^ 2) ^ 0.5 -- 兩個數的平方和再開平方根 end function twice(x) return 2 * x -- 一個數的兩倍 end
_PROMPT = ">lua" -- 修改交互模式中的提示符,默認爲 >
if
then
elseif
end
for
do
in
while
repeat
until
if 條件表達式 then elseif 條件表達式 then end for 控制變量, 終止變量, 步長 do <循環體> end a = {} for i,v in ipairs(a) do <循環體> end while i < 10 do i = i + 1 print(i) end repeat i = 0 i = i + 1 until i > 10
true
false
and
or
not
function
local
nil
nil == nil
是相等的and
和 And
不一樣,lua 區分大小寫true
和 false
false
和 nil
均可以用做表示「真」""
和數字 0
--
--[[]]
---[[ <代碼塊> --]]
--[==[ <多行註釋> ]==]
nil
nil
便可-i
先執行程序塊,後進入交互模式-e
直接執行代碼塊-l
加載庫文件LUA_INIT
的環境變量@文件名
的話,就執行這個文件lua -i -e "hello" script a b arg[0] = "script" arg[1] = "a" arg[-1] = "hello" arg[-2] = "-e" arg[-3] = "-i"
...
三個點,做爲函數參數傳遞時表示傳遞全部參數type()
能夠返回一個值的類型名稱type()
的返回結果永遠是 string
類型的print(type(3)) -- number print(type("a")) -- string print(type({"a", "b", "c"})) -- table print(type(io.read)) -- function print(type(true)) -- boolean
2e2
表示 200long
tonumber()
用於將一個字符串顯式的轉換爲數字類型在 lua 中,有兩個布爾值一個是 true
表示爲「真」,一個是 false
表示爲「假」數組
但,這兩個值不是用來表示條件的惟一值,在 lua 中 除 nil
和 false
外的任何值,均可以用來表示函數
「真」, 包括空字符串 ""
和數字 0
測試
nil
..
字符串鏈接符,用於鏈接兩個字符串,但數字類型使用時須要用空格隔開#
長度操做符,後跟字符串,能夠獲取字符串長度[[]]
在期內的特殊字符不須要轉義[==[ <多行註釋> ]==]
能夠正確打印多行註釋的內容"3" + 4
這樣的值會是 number
類型,發生了運行時隱式轉換print("\97" == "a") -- 在 ASCII 編碼表中,\97 表示爲 a print(type(3 .. "")) -- string print(3..4) --報錯 print(3 .. 4) -- 34 print(#"hello") -- 5 -- 獲取子串,證實字符串是不可變的值 a = "hello" b = a .. " ,world" print(a) -- hello print(b) -- hello, world a = [[ <html> <head><title>蕪湖</title></head> <body></body> </html> ]] a = [==[ --[[ print("多行註釋") print("多行註釋") ]] ]==] print(type("3" + 4)) -- number
tostring()
.. ""
任意數字鏈接一個空字符串便可轉換爲字符串是對象,由自動內存回收器進行分配和釋放編碼
table 沒有固定大小,能夠動態添加元素lua
{}
是 table 構造式,用來建立一個 table指針
#
長度操做符能夠獲取 table 的大小調試
table 中的元素在未被初始化前都是 nil
code
能夠將 table 中的元素賦值 nil
來進行刪除orm
若是 table 中間部分的元素值爲 nil
就說明這是一個有「空隙」的 table
有「空隙」的 table 要使用 table.maxn()
來返回這個函數的最大正索引數
table 能夠用來表示模塊、包、對象
table 中的索引能夠是任何值,除了 nil
table 是匿名的
程序僅保留了對 table 的一個引用
一個僅有 table 的變量和 table 自身並無關係
a.x
等價於 a["x"]
是以字符串爲索引的
a[x]
是以變量 x
爲索引的
a = {} for i = 1, 10 do a[i] = i print(a[i]) end for i = 1, #a do print(a[i]) end print(a[10]) -- 10 print(#a) -- 10 a[10] = nil print(#a) -- 9 a[10000] = 666 print(#a) -- 9 print(table.maxn(a)) -- 10000 a = {} b = {} c = a print(type(a == b)) -- false print(type(a == c)) -- true x = "y" a["x"] = 666 a["y"] = 777 print(a.x) --666 print(a[x]) -- 777
表達式用於表示值
在 lua 中,函數調用,函數定義,數字常量、字面字符串,變量,一元和二元操做符,table 構造式都是表達式
-
負號+
-
減號*
/
%
^
-- % 的技巧 -- x % 1 print(3.13 % 1) -- 獲得小數部分 -- x - x % 1 print(3.14 - 3.14 % 1) -- 獲得整數部分 -- x - x % 0.1 print(3.14 - 3.14 % 0.1) -- 獲得整數部分 + 一位小數部分 -- x - x % 0.01 以此類推,是整數部分 + 兩位小數部分
>
<
>=
<=
==
相等性判斷~=
不等性判斷and
第一個操做數爲假,返回第一個,不然返回第二個or
第一個操做數爲真,返回第一個,不然返回第二個not
只會返回 true
或 false
-- 短路操做的使用技巧 print(x = x or v) -- 初始化一個值,若是 x 爲 nil 沒有被初始化過,就賦值 v -- 等價於 if not x then x = v end -- 實現 C 語言中的三元操做符, a ? b : c print((a and b) or c) -- b 必須爲真,才能夠這樣操做 -- 等價於 if a == true then return b elseif a == false then return c end -- 實現返回兩個數中的較大值 max = (x > y) and x or y -- 由於 lua 將數字視爲「真」 -- 等價於 if x > y then return x else return y end
..
字符串鏈接^
-
負號not
#
*
/
%
+
-
減號..
字符串鏈接>
<
>=
<=
==
~=
and
or
a = {x = 10, y = 20} -- 等價於 a.x = 10, a.y = 20
a = { color = {"red", "green", "blue"} width = 200, height = 300 }
list = nil for line in io.lines() do list = {next = list, value = line} end local l = list while l do print(l.value) l = l.next end
options = {["+"] = "add", ["-"] = "sub", ["*"] = "mul", ["/"] = "div"} print(options["+"]) -- "add"
a, b = 1, 2
nil
a, b = 1, 2 x, y = y, x -- 交換變量 a, b = 1 -- a = 1, b = nil a, b = 1, 2, 3 -- a = 1, b = 2, 3 被拋棄 a, b = f() -- a 接收函數 f 的第一個返回值,b 接收第二個 a, b, c = 0, 0, 0 -- 初始化賦值
do <要執行的內容> end
將要執行的內容包裹在一個塊內local
用來聲明一個局部變量a = 3 b = 0 if a then local a = 5 b = a -- 將 then 塊內的局部變量 a ,保存到全局變量 b 中 print(a) end print(a) -- 3 print(b) -- 5 do -- code block end
if
elseif
else
if 條件表達式 then <執行體> -- 符合條件表達式執行 end if 條件表達式1 then <執行體 1> -- 符合條件表達式 1 執行 elseif 條件表達式2 then <執行體 2> -- 符合條件表達式 2 執行 end if 條件表達式 then <執行體 1> -- 條件表達式爲真時執行 else <執行體 2> -- 條件表達式爲假是執行 end
for
while
條件表達式爲假時退出repeat ... until
條件表達式爲真時推出,條件測試是在循環體以後作的,所以循環體至少會執行一次break
或 return
在循環正常結束前提早結束它for exp1, exp2, exp3 do <循環體> end while 條件表達式 do <循環體> end repeat <循環體> until 條件表達式 a = 20 repeat local a = 0 print(a) until a == 0 -- 可訪問在 repeat 塊內聲明的 a, 而不是全局變量 a
for i = 10, 0, -1 do print(i) end
ipairs()
用來遍歷數組i
每次循環時都會賦予一個新的索引值,v
則是索引值所對應的元素a = {1, 2, 3, 4, 5, 6} for i,v in ipairs(a) do print(i) print(v) end for i,v in pairs(a) do print(i) print(v) end
days = {"第一天", "次日", "第三天"} revdays = {} for i, v in ipairs(days) do revdays[v] = i -- 逆向數組,將數組索引和數組元素調換,可獲取數組元素的位置 end print(revdays["次日"]) -- 獲取次日所在位置
break
用於結束一個循環,跳出內層循環後在外層循環中繼續執行return
用於返回函數結果或簡單的結束函數的執行return
return
return
或 break
後的語句將沒法執行到do ... end
塊包裹 return
,用與調試,即調用函數但不執行函數內容的狀況a = 1 if a then print("hello") break print("world") -- 會報錯 end for i = 1, 10 do print(i) if i > 3 then break -- 只會打印 1 2 3 4 而後就跳出循環了 end end -- 調試 function foo(...) do return end print("執行 foo 函數") -- 不會打印 end foo(1, 2 ,3)
print("hello") -- 用來完成打印任務,視爲一條語句 a = os.date() -- os.date() 用來返回日期,視爲一句表達式
()
()
能夠省略()
print "hello" -- hello print {1, 2, 3} -- 1 2 3 print(os.date) -- 當前日期
function
是建立函數的關鍵字function add
add 是函數的名稱function add(n)
n 是函數的形式參數,簡稱爲形參add(4)
4 是調用 add()
函數時的實際參 ,簡稱爲實參nil
function foo(a, b) return a or b end foo(1) -- a = 1, b = nil foo(1, 2) -- a = 1, b = 2 foo(1, 2, 31) -- a = 1, b = 2, 多餘的 31 被拋棄 -- 面向對象式調用 o.foo(o, x) o:foo(x) -- 與上面的效果同樣,: 冒號操做符,隱式的將 o 做爲第一個參數
string.find("you are cool", "are") -- 5 7 返回找到的字符串的開頭位置和結尾位置 -- 查找數組中的最大元素,並返回這個元素的所在位置 function maximum(a) local val = 1 local max = a[val] for i,v in ipairs(a) do if max < a[i] then max = a[i] val = i end end return max, val end a = {1, 2, 55, 22, 29, 4} maximum(a)
nil
來補充缺失的值function foo() end function foo1() return "a" end function foo2() return "a", "b" end -- 第一種狀況,最後(或僅有)的一個表達式 x, y = foo1() -- x = a, y = b -- 第二種狀況,沒有返回值 x = foo() -- nil -- 第二種狀況,沒有返回足夠多的返回值 x, y, z = foo1() -- x = a, y = b, z = nil -- 第三種狀況,不是表達式中的最後一個元素 x, y = foo2(), 10 -- x = a, y = 10
function foo() end function foo1() return "a" end function foo2() return "a", "b" end -- 第四種狀況,做爲 print 函數中的最後一個(或僅有的)實參 print(foo()) -- nil print(foo1()) -- "a" print(foo2()) -- "a" "b" print(foo1() .. "test") -- "atest" print(foo2() .. "test") -- "atest"
function foo() end function foo1() return "a" end function foo2() return "a", "b" end -- 函數調用是 table 中的最後一個元素 a = {foo2()} -- a = {"a", "b"} a = {foo2(), 10} -- a = {"a", 10}
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 ,應該是強制返回了一個未初始化的值,由於 foo0() 沒有返回值
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")
function unpack(t, i) i = i or 1 if t[i] then return t[i], unpack(t, i + 1) end end
...
三個點表示該函數接收不一樣數量的實參...
三個點,此時 ...
三個點是做爲一個表達式使用的...
三個點的行爲相似一個具備多重返回值的函數,它返回的是當前函數的全部變長參數select
時,必須傳入一個固定參數 selector
(選擇開關) 和一系列變長參數selector
爲數字 n ,那麼 select
返回它的第 n 個可變實參select
只能爲字符串 "#"
,這樣 select
會返回變長參數的總數,包括 nil
-- 返回全部參數的和 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) ) end
由於,目前只學到第五章函數篇,因此只有前五章的複習彙總,很基礎,也很重要,也祝願你們能夠踏踏實實地打好地基。