Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get the position of something in a table?

Asked by
Protall 10
10 years ago
tablee = {hi = 3, bean = 2, cake = 1}

tablee2 = {"hi", "bean", "cake"}

toomany = {
    hi = 2;
    bean = 4;
    cake = 5;
}

for i,v in pairs(toomany) do
    if v <= tablee[i] then
        print(i.." has too many and I don't know how to get rid of it in tablee2")
        -- table.remove(tablee2, i) doesn't work because i returns "hi" rather than its position in the table
    end
end

The above code needs to get rid of the things in tablee2 when there are too many of them in tablee. This is the method I have to use, because the context I'm using it in forces me to use it like this. I just made this example simpler but similar.

1 answer

Log in to vote
0
Answered by 10 years ago

i does not return the object in the table. i returns the index.

tablee[i] returns the object in the table.

Ad

Answer this question