Xikini
Xikini

Reputation: 25

Lua, how to access an index that uses an array

How can I check an array inside an index? [{4, 8}] to confirm if 'vocation' aka 8 exists?

local outfits = {
    [7995] = {
            [{1, 5}] = {94210, 1},
            [{2, 6}] = {94210, 1},
            [{3, 7}] = {94210, 1},
            [{4, 8}] = {94210, 1}
    }
}

local item = 7995
local vocation = 8

if outfits[item] then
    local index = outfits[item]
    --for i = 1, #index do
    --  for n = 1, #index[i]
    --  if index[i]
    -- ????
end

Upvotes: 1

Views: 116

Answers (1)

Nifim
Nifim

Reputation: 5031

You just need to iterate using pairs rather than a basic for loop. With pairs you get your key value pairs and can then loop over the key to inspect it's contents.

local found = 0

if outfits[item] then
    local value = outfits[item]
    for k, v in pairs(value) do
        for n = 1, #k do
            if k[n] == vocation then
                found = k
                break;
            end
        end
    end
end
print(outfits[item][found][1])

That said this is not the a very efficient method of storing values for look up and wont scale well for larger groups of record.

Upvotes: 1

Related Questions