1

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

1 Answer 1

1

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.

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.