lua 是动态类型的语言html
print(type("Hello")) --string print(type(666)) --number a = print print(type(a)) --function a = 111 print(type(a)) --number print(type(b)) --nil print(type(string)) -- table 不太懂为何 print(type(type(X))) -- string
nil (空)数组
boolean (布尔)安全
number (数字)数据结构
科学计数法函数
a = 3.2e2 print(a) --320
string (字符串)学习
修改子串测试
a = "one string" b = string.gsub(a, "one", "another") -- 字符串替换函数 print(b) -- another string print(a) -- one string
字面字符串编码
a = 'Hello' b = "World" c = "I'm a student" d = "I\'m a student'"
转义序列lua
print("one line\nnext line\"in quotes\",'in quotes'") --[[ one line next line "in quotes",'in quotes' --]]
能够经过数值指定字符串中的字符操作系统
print('a' == '\97') -- true
page = [[ <html> <head> <title>Lua Study</title> </head> <body> <a href="http://www.lua.org">Lua 学习 </a> </body> </html> ]]
lua 提供了运行时数字和字符串的自动转换
print("10" + 1) --字符串10会被自动转换为数字10,结果打印为11 print("Hello" + 1) -- 会报错
字符串链接符 ..
print("A".."=".."B") -- A=B print(10..24) -- 会报错 print(10 .. 24) -- 1024
长度操做符 #
print(#"Hello") -- 5
tonumber
line = io.read() -- 读取一行 n = tonumber(line) if n = nil then error(line .. " is not a valid number") else print(n * 2) end
tostring
a = 2333 print(tostring(2333) == "2333") -- true print(a .. "" == "2333") -- true
举例: io.read
使用字符串 read 做为 key 来索引 table io
lua 不会暗中产生 table 的副本,或者建立新的 table
在 lua 中不须要声明一个 table
a = {} -- 建立了一个 Table 把它的引用存储到 a 这个变量里,那么 a 就是一个 table 类型的变量了 k = "x" a[k] = 10 -- 新条目, key = x, value = 10 在 a 这个 table 添加了一个元素,这个 table 的索引是字符串索引,这个新生成的条目的值就是 10,它的索引是 x a[20] = "great" -- 新条目, key = 20, value = "great" print(a["x"]) -- 10 k = 20 print(a[k]) -- great a["x"] = a["x"] + 1 print(a["x"]) -- 11
table 永远是匿名的
a = {} a["x"] = 10 b = a -- b 与 a 引用了一样一个 table print(b["x"]) -- 10 a = nil print(a["x"]) --报错 print(b["x"]) --10 由于如今只有 b 在引用 table ,而 a 已经清除了对 table 的引用 b = nil -- 如今 a 和 b 都没有对 table 引用了
全部 table 均可以经过不一样类型的索引来访问它的值
-- for 循环 ,索引从1开始,与C或其余语言从0开始不一样, do 里面的代码块是循环体 t = {} for i = 1, 1000 do t[i] = 2 * i -- 值是索引的两倍 end print(t[8]) -- 16 t["x"] = 10 print(t["x"]) --10 print(t["y"]) -- nil t["x"] = nil -- 删除了 table 中索引为 x 的元素
lua table 访问的两种写法(语法糖)
a.x -- 表示为 a["x"] 以字符串 x 为索引 table a[x] -- 以变量 x 的 值 为索引 table a = {} x = "y" -- x 已经被赋值为"y" 因此变量 x 的值为字符串 y ,索引 x 变量就是索引字符串 y a[x] = 10 print(a.x) -- 索引为字符串 x --> nil print(a["x"]) -- 索引为字符串 x --> nil print(a[x]) -- 索引为变量 x --> 10 print("\n") print(a.y) -- 索引为字符串 y --> 10 print(a["y"]) -- 索引为字符串 y --> 10 print(a[y]) -- 索引为变量 y --> nil
表示传统的数组或线性表
长度操做符 # 对于 table 的做用
t = {} for i = 1, i = 666 do t[i] = i * 2 end print(#t) -- 666, 至关于这个 table 有 666 个元素(或者称这个 table 的大小为 666) for i = 1, #t do print(t[i]) end print(t[#t]) -- 打印 table 最后一个值 t[#t] = nil -- 删除 table 的最后一个值 print(#t) -- 如今 table 的长度就是 665 个 t[#t + 1] = 8888 -- 给 table 再加一个元素 print(#t) -- 如今 table 的长度是 666 个 print(t[#t]) -- 如今 table 的最后一个元素就是 8888 了
数组的实际大小
a = {} a[10000] = 1 print(#a) -- 0 print(a[10000]) -- 1 print(table.maxn(a)) --> 10000
对于索引类型不明确时进行显式转换
i = 10 j = "10" k = "+10" t = {} t[i] = "one value" t[j] = "another value" t[k] = "yet another value" print(t[i]) -- one value print(t[j]) -- another value print(tonumber(t[k])) -- one value print(tonumber[t[j]]) -- one value print(t[tonumber(k)]) print(t[tonumber(j)])