I am losing my mind over this very simple thing. These don't work:
next(table)==nil
table=={}
table==0
table==nil
This should be so simple but it's not working!! :(



Hack solution:
local empty = true
for g in all(grasses) do
empty = false
break
end
if (empty) then
level += 1
load_level(level)
end
still dont properly know how to check if a table is empty



"#table" is the length of the table, so you could check if "#table==0". I think that should work if you only want to know whether it's empty.



Actually, after a little testing, this:
table={nil,2,nil} print(#table) |
showed 0, although I'm not sure why. And it doesn't seem like I can iterate through that table with
for t in all(table) do |
So now I'm stumped, too. Hopefully someone else can shed some light on this.



> for k,v in pairs({1,2,3}) do print(k.." "..v) end
1 1
2 2
3 3
> for k,v in pairs({1,2,3}) do print(k.." "..v) end
2 2
When you set elements 1 and 3 to nil you effectively erase index 1 and 3. So "#T", intuitively, might therefore its behavior be testing for a series starting at one; thus, also miss non-numeric indices; indeed
> t={1,2,3}
> t.x=4
> print(#t)
3
> for k,v in pairs(t) do print(k.." "..v) end
1 1
2 2
3 3
x 4
So the #t function seems only relevant if using it as a stack or queue with always keeping index 1 full, which those add del all etc. functions are also oriented around



for tables where integer-valued keys don't form a contiguous sequence starting at 1, the result of # is implementation-defined, so it's often not helpful for those. # also isn't useful for finding non-integer keys. more information on #'s behavior is in the lua manual.
here's an example of a function that implements an emptiness check:
function is_empty(t) for _,_ in pairs(t) do return false end return true end |
try testing it with:
for x in all({{}, {1}, {1, 2}, {x='y'}}) do print(is_empty(x)) end |



as for why next fails, pico-8 uses its own set of primitives distinct from lua's default ones, and next isn't one of them.
as for why table=={} fails, lua (as well as many other languages) goes against your intuition that this should compare two tables for structural equality; lua's table values are actually references to tables, and by default == compares these references. {} is a newly created table, and newly created tables are referentially distinct from previously created ones.
[Please log in to post a comment]