Bugs / Quirk (?) of the len operator (#) on table with nil values

Published 2023-06-12

Hey.. Can someone please explain what's going on here for me?

Here's a test cart that prints the content of table 'x' (a table with the first 16 offsets set to 1 by default) and the value of '#x'..

LEFT/RIGHT changes the offset of table 'x'
X sets the offset to 1 / Z (O) sets the offset to nil


If I set certain values to nil in a table, then len will start returning 0 even though there are still items inside the table. This seems to happen when a 'certain' number of items, including the item in offset=1 are set to nil, and then offset 0 or offset -1 are set to nil:

This doesn't seem entirely consistent.. Here, if I start at offset 7 instead of offset 13, then len still returns the highest index with non-nil data. Even if I'm setting offset 0 or offset -1 to nil:

Is this a bug or just a quirk of lua?
If so, does anyone have a list of rules for this behavior so I can design around it?