What would be the correct way to check if a value exists in the table in Lua?

2
if not name in Namez do
    table.insert(Namez,name)
end

What would be the correct way to check if a name is not in the table?

    
asked by anonymous 27.10.2018 / 17:02

1 answer

2

Nothing is ready, you can create a function that does this, like this:

local function contains(tabela, valor)
    for i = 1, #tabela do
        if tabela[i] == valor then 
            return true
        end
    end
    return false
end

local names = {"joão", "maria", "josé"}
if not contains(names, "carlos") then
    table.insert(names, "carlos")
end
for i = 1, #names do
    print(names[i])
end

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

    
27.10.2018 / 17:08