How would I get them next item in a table?

Example code:

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?

Try this:

local table = {
  [1] = "abc",
  [32] = "ez clap",
  [6969] = "amogus"
}

for _, v in pairs(table) do
  print(v)
end

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

local nextKey, nextValue = next(tables, currentKey)

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
4 Likes

sorry to bother, but how would i get previous item in a table?

You need to get the index of the original item.

i = table.find(mytable,“originalitem”) – this returns the index, if it exists.
Then you can just do
prevItem = mytable(i-1)

so no built in way like next to get previous item?

Sounds like you’re trying to traverse a table by going next, next, next.
You can do that, but it’ll need to be in a loop.
Otherwise, I don’t think so