Scripting problem relating to tables

local StageDifficultyIncrements = {
    [5] = {11372824927},
    [2] = {11372909939},
}

If I have the number 11372824927, how do I find that it is connected to the number 5?
Thanks - lux

if table.find(StageDifficultyIncrements[5], 11372824927) then
--code
end

I have no idea if it works but it works like this :wink:

not sure but i think all you need to do is:

StageDifficultyIncrements[5]

You would have to iterate through that table using a for loop. For this case, I’ll make a function that returns the key that the number is hiding in. Since the numbers are also in tables, You also have to search through the tables inside the main table.

local StageDifficultyIncrements = {
    [5] = {11372824927},
    [2] = {11372909939},
}

local function getKeyNumberIsHidingIn(tbl: {[number]: {number}}, numberSearching: number): number?
    for key, subTable in pairs(tbl) do
        for _, number in pairs(subTable) do
            if number == numberSearching then
                return key
            end
        end
    end
end

local key = getKeyNumberIsHidingIn(StageDifficultyIncrements, 11372824927)
print(key) -- outputs 5

Maybe that works too

inser30character

you can use table.find to use this, you could use @bhristt 's solution, but I believe it will run slower due to using pairs and not just table.find, I could be wrong tho

local StageDifficultyIncrements = {
    [5] = {11372824927},
    [2] = {11372909939},
}

local function SearchTable(Input, Value)
    for i,v in next, Input do
        if table.find(v, Value) then
            return i
        end
    end
end

print(SearchTable(StageDifficultyIncrements, 11372824927)) --> 5
print(SearchTable(StageDifficultyIncrements, 11372909939)) --> 2
2 Likes

Brilliant! Thank you all so much for the help! These all work amazing.

table.find and using a for loop do the same thing and would both have the same run time, but i guess this function is written more conveniently.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.