-- Lua 唯⼀的數據結構 "Table", 可創造出不同的類型
-- 指定具體的key時,table為'字典'。
-- myTable1 = { key1 = value1, key2 = value2, ...}
-- table沒有key時,table為'數組'。
-- myTable2 = {"value1", "value2", ...}
mytable = {1, 2, 5}
print(mytable[3])
-- table中的key只能是字符串,這裡的a, b, c都是字符串,不能加上引號
-- 如果透過key來訪問table的值,key必須帶上引號
Table = {a = "APPLE", b = "BIRD", c = "CAT"}
print(Table["b"]) --BIRD
--錯誤寫法
--mytable = {1 = "apple", 2 = "bird", 3 = "cat"}
--mytable = {"a" = "apple", "b" = "bird", "c" = "cat"}
--當table中沒有指定key時,key會從1開始自增
mytable = {a = "apple", b = "bird", "cat", "fox"}
print(mytable[1]) --cat
print(mytable[2]) --fox
print(mytable[3]) --nil
print(mytable["a"]) --apple
print(mytable.b) --bird
print("----Concatenation----")
print(table.concat(mytable)) --catfox
print(table.concat(mytable,",")) --cat,fox
print(table.concat(mytable,",",2)) --fox
-- Insert
print("----Insert----")
colors = { "red", "green", "blue" }
table.insert(colors, "orange") -- insert to end of table
print(colors[#colors]) --orange
table.insert(colors, 2, "pink") -- insert to index #2
print(colors[2]) --pink
-- Remove
print("----Remove----")
table.remove(colors) -- remove latest value
table.remove(colors, 2) -- remove index #2 value
print(colors[2]) --green
mytable = {}
print(mytable[1]) --nil
print("----Sort----")
myTable = {"banana","orange","apple","grapes"}
table.sort(myTable)
print(table.concat(myTable,", ")) -- apple, banana, grapes, orange
print("----For loop with table----")
LoopTable = {2,4,6,8,10,12,14}
for i=1, #LoopTable, 2 do
print(LoopTable[i])
end -- 2,6,10,14
print("-------------")
for k,v in pairs(LoopTable) do
print("key:"..k.." value:"..v..",")
end -- key: 1 value:2, ...
print("-------------")
for k in pairs(LoopTable) do
print("key:"..k..",")
end -- get key only
print("-------------")
for k,v in pairs(LoopTable) do
print("value:"..v..",")
end -- get value only
-- 元表Metatable
-- We cannot add tables, we cannot compare tables, and we cannot call a table.
-- Use 元表Metatable do to add, compare, call.
-- 每個 table 都可以設定一個 metatable,當初始創建一個 table 時,這 table 是沒有 metatable 的。
print("----元表Metatable----")
-- setmetatable(table,metatable): 對指定 table 設置元表(metatable),如果元表(metatable)中存在 __metatable 键值,setmetatable 會失败。
-- getmetatable(table): 返回指定對象的元表(metatable)。
mytable = {} -- 普通表
mymetatable = {} -- 元表
setmetatable(mytable,mymetatable) -- 把 mymetatable 設為 mytable 的元表
-- 以上動作可以寫成一行
mytable = setmetatable({},{})
-- 當透過key來訪問table的值,如果這個key没有value,Lua會尋找table的metatable(假定有metatable)中的__index 键。如果__index包含一個表格,Lua會在表格中查找相應的key。
mtTable = {testName = "Boot SoC to gOS", commands = {}, behaviorTag = "retry+5"}
newTable1 = setmetatable({},{ __index = mtTable})
newTable2 = setmetatable({},{ __index = mtTable})
print(newTable1.testName) --Boot SoC to gOS
print(newTable2.testName) --Boot SoC to gOS
newTable1.testName = "Power on"
newTable2.testName = "Power off"
print(newTable1.testName) --Power on
print(newTable2.testName) --Power off
print("-------------")
-- 如果__index包含一個函數,Lua就會調用那個函數,table和key作爲參數傳遞給函數。
mytable = setmetatable({key1 = "value1"}, {
__index = function(mytable, key)
if key == "key2" then
return "metatablevalue"
else
return nil
end
end
})
print(mytable.key1, mytable.key2)
-- 可簡化成
mytable = setmetatable({key1 = "value1"}, { __index = { key2 = "metatablevalue" } })
print(mytable.key1,mytable.key2)
print("-------------")
--basic table
local colors = { "red", "green", "blue" }
print(colors[1]) --red
print(colors[2]) --green
print(colors[3]) --blue
--using a loop to iterate though your table
for i=1, #colors do
print(colors[i])
end
-- 2 Dimensional Table
-- tables within tables
print("----2 Dimensional Table----")
local data = {
{ "billy", 12 },
{ "john", 20 },
{ "andy", 65 }
}
print(#data .. " rows") -- 3 rows
for a=1, #data do
print(data[a][1] .. " is " .. data[a][2] .. " years old")
end
-- Key Tables
-- 2 dimensional tables are not suited to data with different types, instead uses keys for tables
print("----Key Tables----")
local teams = {
["A"] = 12,
["B"] = 15,
["C"] = 8
}
print(teams["C"]) -- 8
--insert into key table
teams["D"] = 1
--remove key from table
teams["A"] = nil
for key,value in pairs(teams) do
print(key .. ":" .. value)
end
To embed this program on your website, copy the following code and paste it into your website's HTML: