Lua:搜索word - Array中的数组

时间:2017-09-14 13:31:04

标签: arrays lua

我正在编程Lua,而且我对这种语言很陌生。我有一个问题,我有一个汽车的模型。然后我必须在数组内搜索包含此模型的数组并返回它的成本。例如,如果我得到了型号名称" zion"它需要返回它的成本。真的无法弄清楚如何做到这一点。希望有人能帮助我找到解决方案。

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"},
}

所以我得到了模特" zion"这是第二个阵列。希望你能帮忙打印成本= 79000

1 个答案:

答案 0 :(得分:3)

试试这个:

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