2017-09-14 119 views
0

我現在正在編程Lua,我對這種語言很新。我有一個問題,我有一輛汽車的模型。然後,我必須在數組內搜索包含此模型的數組,並返回它的成本。舉例來說,如果我有型號名稱「錫安」,它需要返回它的成本。真的不知道如何做到這一點。希望有人能幫我解決問題。Lua:搜索詞 - 數組內的數組

local vehicles = { 
{name = "Honda Civic", costs = 99000, description = {}, model = "blista2"}, 
{name = "Peugeot 206 GTI", costs = 79000, description = {}, model = "blista"}, 
{name = "Golf R32", costs = 300000, description = {}, model = "zion"}, 
{name = "Mercedes Brabus", costs = 2000000, description = {}, model = "schafter2"}, 
{name = "f620", costs = 80000, description = {}, model = "f620"}, 
{name = "Toyota supra", costs = 290000, description = {}, model = "massacro2"}, 
} 

所以我得到了模型「錫安」,這是第二陣列。希望你能幫助獲得打印成本= 79000

+0

錫安費300000,並不79000 ... – lhf

+1

是有一個元素作爲密鑰(例如:如果每一個模型是不同勢,型號可以是關鍵,你可以重建臺車) –

回答

3

試試這個:

for k,v in pairs(vehicles) do 
    if v.model == "zion" then 
     print(v.costs) 
    end 
end