Corona SDK使用Lua語言來撰寫,因此想要開發App除了了解Corona的Api以外,Lua的語言也是需要好好了解,才可以寫出簡單易了解,又易維護的程式。今天就來好好介紹一下,Lua語言中的百寶袋 -- ”Table”。 Table是什麼?當然不是桌子啦(真冷~~),Lua裡的Table就跟其他程式語言的Array相當類似,但是功能更強大!為什麼說功能更強大呢?因為這個Table什麼都可以裝,舉凡常數、變數、字串、boolean、函數等都可以塞進去,而且不一定要像Array一樣由數字來索引,也可以像字典(dictionaries)一樣,用名字來索引。厲害吧~~ 嚇到了吧~ 哈哈 以下介紹Table怎麼使用: 一. Table宣告宣告的方法很簡單,如下:
-----------------------------------------
local nameTable = {}
-----------------------------------------這樣就宣告出一個nameTable的空Table了。 那麼如果想要使用多維陣列的話,就如以下範例:-----------------------------------------
local nameTable = {}
nameTable[1] = {}
nameTable[1][1] = “Big”
-----------------------------------------nameTable[1] = {}
nameTable[1][1] = “Big”
這樣就宣告出一個多維的陣列了。
二. 陣列(Array)用法的Table可有以下三種
a.
-----------------------------------------
local nameTable = {}
nameTable[1] = “Big”
nameTable[2] = “Amy”
nameTable[3] = “Jobs”
nameTable[4] = “Steve”
print( nameTable[2] )
-- Output: “Amy”
-----------------------------------------nameTable[1] = “Big”
nameTable[2] = “Amy”
nameTable[3] = “Jobs”
nameTable[4] = “Steve”
print( nameTable[2] )
-- Output: “Amy”
b.
-----------------------------------------
local nameTable = {
[1] = “Big”,
[2] = “Amy”,
[3] = “Jobs”,
[4] = “Steve”
}
print( nameTable[2] ) -- output: “Amy”
-----------------------------------------[1] = “Big”,
[2] = “Amy”,
[3] = “Jobs”,
[4] = “Steve”
}
print( nameTable[2] ) -- output: “Amy”
c.
-----------------------------------------
local nameTable = {“Big”, “Amy”, “Jobs”, “Steve”}
print( nameTable[2] )
-- Output: “Amy”
-----------------------------------------print( nameTable[2] )
-- Output: “Amy”
採用以上語法,就能夠創造出一個Table,並且對於Table的內容加以宣告,從上面例子可知,Lua的Table引數是從1開始的。另外如果你想知道這個Table裡面有多少的東西,可以採用" #Table "的語法得知,如以下範例:-----------------------------------------
print( #nameTable )
-- Output: 4
------------------------------------------- Output: 4
三. 字典(dictionaries)查找方式的Table 這部份我們直接來看範例比較快:-----------------------------------------
-- 宣告出nameTable陣列及初始內容
local nameTable = {
host = “Big”,
wife = “Amy”,
son = “Jobs”,
friend = “Steve”
}
--若要額外加入內容
nameTable.guest = “Bill Gates”
print( nameTable.host ) -- output: “Big”
print( nameTable[“wife”] ) -- output: “Amy”
print( nameTable[“guest”] ) -- output: “Bill Gates”
-----------------------------------------local nameTable = {
host = “Big”,
wife = “Amy”,
son = “Jobs”,
friend = “Steve”
}
--若要額外加入內容
nameTable.guest = “Bill Gates”
print( nameTable.host ) -- output: “Big”
print( nameTable[“wife”] ) -- output: “Amy”
print( nameTable[“guest”] ) -- output: “Bill Gates”
這樣應該很清楚,Table怎麼使用了吧。在字典用法中,可以直接用”Table.子內容名” 去取得你所要的資料,也可以採用Array的方式使用”Table[“子內容名”]”的方式取得資料。 但是,如果採用字典形式的Table,則使用 #nameTable是無法取得Table的資料筆數的。#nameTable只限於Array形式的Table使用,但是,如果想要知道字典形式的Table資料筆數怎半呢?變成要用Lua裡的paris()函數:-----------------------------------------
-- 宣告出nameTable陣列及初始內容
local nameTable = {
host = “Big”,
wife = “Amy”,
son = “Jobs”,
friend = “Steve”,
guest = “Bill Gates”
}
for key, value in paris(namrTable) do
print(key, ‘=’, value)
end
--OUTPUT:
host = Big
wife = Amy
son = Jobs
friend = Steve
guest = Bill Gates
-----------------------------------------local nameTable = {
host = “Big”,
wife = “Amy”,
son = “Jobs”,
friend = “Steve”,
guest = “Bill Gates”
}
for key, value in paris(namrTable) do
print(key, ‘=’, value)
end
--OUTPUT:
host = Big
wife = Amy
son = Jobs
friend = Steve
guest = Bill Gates
再來以下介紹Table的比較高階用法了。 我在上面說過,Table就像是百寶袋,什麼東西都可以塞進去,那可不可以在塞進一個Table進去呢? 當然可以,看看以下範例:-----------------------------------------
-- 宣告出nameTable陣列及初始內容
local PartyTable = {
{host = “Big” , people_lim = 20, alcohol = false},
{host = “Amy”, people_lim = 10, alcohol = true},
{host = “Jobs” , people_lim = 200, alcohol = false}
}
print( PartyTable[1].name ) -- output: Big
print( PartyTable[3][“people_lim”] ) -- output: 200
-----------------------------------------local PartyTable = {
{host = “Big” , people_lim = 20, alcohol = false},
{host = “Amy”, people_lim = 10, alcohol = true},
{host = “Jobs” , people_lim = 200, alcohol = false}
}
print( PartyTable[1].name ) -- output: Big
print( PartyTable[3][“people_lim”] ) -- output: 200
這樣對於管理一些資訊相當好用。 那麼我上面也說過,也可以塞一個函數(Function)進去,是怎麼一回事呢?我們先來看看以下範例吧:-----------------------------------------
local function SayHi( name )
print( “Hi”.. name .. ”, how are you?”)
end
local FunctionTable = {
name = “Big”,
func = SayHi
}
FunctionTable.func(FunctionTable.name) -- output: Hi Big, how are you?
-----------------------------------------print( “Hi”.. name .. ”, how are you?”)
end
local FunctionTable = {
name = “Big”,
func = SayHi
}
FunctionTable.func(FunctionTable.name) -- output: Hi Big, how are you?
當然,如果function簡單也可以直接塞在Table裡面:-----------------------------------------
local FunctionTable = {
“Big”,
function(name) print ( “Hi” .. name .. “, how are you?”); end
}
FunctionTable[2]( FunctionTable[1] ) -- output: Hi Big, how are you?
-----------------------------------------“Big”,
function(name) print ( “Hi” .. name .. “, how are you?”); end
}
FunctionTable[2]( FunctionTable[1] ) -- output: Hi Big, how are you?
以上Table的使用方法,應當要視不同使用時機,以及個人程式風格而定,並沒有所謂何者好何者壞,大家有經驗之後自然能夠體會。
local nameTable = {
回覆刪除[1] = “Big”
[2] = “Amy”
[3] = “Jobs”
[4] = “Steve”
}
这些后面要加个,否则会出错
local nameTable = {
[1] = “Big”,
[2] = “Amy”,
[3] = “Jobs”,
[4] = “Steve”
}
Thanks a lot~~
回覆刪除