How would I code a function to iterate through one “pages” worth of data? Sample code would be ideal…
So say we image the size of a page is 5 items. If we had a lua table with 18 items it would need to print out:
- Page 1: 1 to 5
- Page 2: 6 to 10
- Page 3: 11 to 15
- Page 4: 16 to 18
So assume the data is something like:
local data = {}
data["dog"] = {1,2,3}
data["cat"] = {1,2,3}
data["mouse"] = {1,2,3}
data["pig"] = {1,2,3}
.
.
.
How would one code the function that would do the equivalent of this:
function printPage (myTable, pageSize, pageNum)
-- find items in "myTable"
end
So in fact I’m not even sure if a Lua table used as a dictionary can even do this? There is no specific ordering is there in such a table, so how would you be sure the order would be the same when you come back to print page 2?
The
nextfunction allows you to go through a table in an order (albeit an unpredictable one). For example:I know this isn’t quite in the form you were after, but I’m sure it can be adapted quite easily.
The
nextfunction returns the key after the one provided in the table, along with its value. When the end of the table is reached, it returnsnil. If you providenilas the second parameter, it returns the first key and value in the table. It’s also documented in Corona, although it appears to be identical.