I'm trying to get the size of a table. While testing, I have this code assigned to a button press:
listSize = table.getsize(undoList)
90% of the time it seems to have the desired results, correctly showing the size of the table, but sometimes it shows the size to be larger than it is according to the Lua Memory window.
Am I misunderstanding how to use this?
Inside Playdate says:
Returns the size of the given table as a multiple values (arrayCount, hashCount).
Is the code adding the two values? What is the difference between them? Which one do I want and how do I only return the value I want?
I'm not certain, but it's possible the 10% of the time you see incorrect results it's because the garbage collector hasn't yet finished running for this cycle. If you force GC by calling the Lua function collectgarbage(), do you get more consistent results?
To capture multiple return values, you just need a little extra syntax:
local arrayCount, hashCount = table.getsize(table)
Unfortunately this doesn't seem to fix my problem.
I'm guessing I should be forcing garbage collection immediately before fetching the table size or after adding to the table?
I've tried both but seem to get the same results.
It sounds like I'm using getsize correctly so maybe there's a problem elsewhere in my code.
The thing is, nowhere in my code is an entry ever removed from my table. So could the table size be reading as 8, for example, and then 6 when being read later?