Hi, I am facing a small issue with string indexed arrays.
I need to "intercept" a table (t, loaded from json), remove one or more entries (according to their index), and pass it through the next step of code if there is some entry left (there could be some).
I thought I could just nil the entries (as in example below) and later detect if t is empty to proceed:
the problem is that t is not empty, and I can't use the lenght operator # as a parameter because indexes are strings.
How do I check if t is "empty"? Or how do I remove the entries I need to remove from it, to leave t empty (if there are no other entries left) ?
t = {
m = {1,3,4,5,"pp"},
n= {"foo"}
}
t["n"] = nil
t["m"] = nil
print("T", t, t == {}) -- t == {} is false, why?
for i,v in pairs(t) do
print(i,v)
for j,k in pairs(v) do
print(j,k)
end
end |
Thank you
[edit: I noticed that I get nil if I use getmetatable (t) on the empy table, that seems to solve my problem ]
Comments
However I found a workaround, I hope it's enough
Thank you
Fragmenter - animated loop machine and IKONOMIKON - the memory game
So the "getmetatable workaround" is the only way to understand if a table is empty?
Likes: john26
Fragmenter - animated loop machine and IKONOMIKON - the memory game
even there it is written that "In particular, you can use next(t) to check whether a table is empty. "
Likes: john26, pie
Fragmenter - animated loop machine and IKONOMIKON - the memory game