local table = {
[1] = "abc",
[32] = "ez clap",
[6969] = "amogus"
}
print(table[1])
print(table[2]) -- issue would start here
The only thing I’m not replicating here is the table is set to {}, and objects are added to it by a for i,v in pairs loop. I have no indication of the order, the order is set by a value inside of the object. So if I had numbers like the ones I set here, how would I get the next object automatically, without typing in the actual numbers, 32, 6969, 1, etc?
I mean I could do that, but how would I index the next number automatically.
Like lets say theres 3 numbers
4
5
17
Instead of using table[4], table[5], etc
How would I use a variable to get it so the variable refers to the current lowest number in order so I don’t have to type in the number
next. That’s the literal function name by the way.
local foobar = {[1] = "abc", [32] = "clap", [6000] = "garply"}
local keyIn, valueIn = next(foobar)
local keyNext, valueNext = next(foobar, keyIn)
Dictionaries and arrays with gaps don’t have order so this is the closest thing you have to fetching the next key of a table. If you’re predictably only filling the bracket portion with numbers but you don’t have gapless arrays then you could sort the keys if you want “order” but that’s a particularly overbearing solution that doesn’t have too much applicability. Depends on your use case.
local function getGapsArrayOrdering(gapArray: {any}): {any}
local orderedKeys = {}
for key, _ in pairs(gapArray) do
table.insert(orderedKeys, key)
end
return orderedKeys
end
-- You can then "order" your array with gaps
local foobar = {[6000] = "garply", [1] = "abc", [32] = "clap"}
local foobarKeyOrder = getGapsArrayOrdering(foobar)
print(foobar[foobarKeyOrder[1]]) --> clap
print(foobar[foobarKeyOrder[2]]) --> garply