0

Im programming Lua at the moment and im pretty new to this language. Ive got a problem where i got a model of a car. Then i have to search inside an array for an array that contains this model and return the costs of it. So example if i got the model name "zion" it need to returns the costs of it. Really cant figure out how to do this. Hope someone can help me to a solution.

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

So i got the model "zion" which is second array. Hope you can help to get printed the costs = 79000

2
  • zion costs 300000, not 79000... Commented Sep 14, 2017 at 14:02
  • 1
    is there one element as the key?(eg: if every model is diffrent, model can be the key, and you can reconstuct the table vehicles) Commented Sep 15, 2017 at 0:53

1 Answer 1

3

Try this:

for k,v in pairs(vehicles) do
    if v.model == "zion" then
        print(v.costs)
    end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.