BulleTTime Posted May 20, 2009 Share Posted May 20, 2009 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
Guest Posted May 20, 2009 Share Posted May 20, 2009 (edited) 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 May 21, 2009 by Guest Link to comment
darkdreamingdan Posted May 21, 2009 Share Posted May 21, 2009 Lua has a built in next() function, lol Link to comment
BulleTTime Posted May 21, 2009 Author Share Posted May 21, 2009 would u like to show a working example using next() talidan ? (on the same table structure as above) thanks Link to comment
lil Toady Posted May 25, 2009 Share Posted May 25, 2009 http://www.lua.org/manual/5.1/manual.html#pdf-next Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now