Jump to content

Get next table slot


Recommended Posts

Hi, i was wondering how i could get the next absolute slot of a table.

i would need a function that is called like this:

nextslot = getNextSlot(slot)

For example i would have a table like below:

  
table1 = {} 
  
table[3] = "filled" 
table[7] = "filled" 
table[9] = "filled" 
  

Now lets say that i got number "9", then the function should return "3" and when i got the number "3" it should return "7"

well thanks for any help

Link to comment

function getNextSlot(startslot, table) for slot, content in ipairs(table) do if (slot <= startslot) then slot = startslot + 1 elseif (content ~= '') then return slot end end return false end 

function getPreviousSlot(startslot, table) for slot, content in ipairs(table) do if (not initialize) then slot = startslot initialize = true elseif (content ~= '') then return lot end slot = slot -2 end return false end 

maybe like this ;)

you can just add some code to get what you really wanted.

function getNextSlotByLoopThrough(index, table) 
local result = getNextSlot(index, table) 
if (not result) then  
for slot, content in ipairs(table) do if (content ~= '') then result = slot break end if (slot == index) then break end end 
end 
return result 
end 

the first two functions will return the next or previous index. if all following or previous indizes are empty, it just returns falls.

the third function will only return false in case that the whole table is empty. it starts from the index you entered and if all following indizes of the table are empty, it'll just start searching from the first index on. if the index you started from is the only index with content, the function will return this index

Edited by Guest
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...