Re:Again on 0-based vs. 1-based indexing
by mjevans on Wednesday January 20, 2021 @11:30AM
(#25848358)
That sounds like an implementation bug in the name of speed or memory size rather than a programming correctness bug. What's the correct behavior if the 'table' doesn't have any integers as keys, but instead other elements such as strings?
Re:Again on 0-based vs. 1-based indexing
by lrossi on Wednesday January 20, 2021 @03:02PM
(#25851214)
You can use strings as keys. In fact, that’s how you can create objects. Also negative integers can be used as keys. But array operations will malfunction if you attempt to apply them to such a table.
Here are some examples: http://www.lua.org/pil/2.5.html and http://www.lua.org/pil/11.1.html
The tricky thing is that the length operator in Lua (#) actually returns the largest integer index in the table. If you use 1-based arrays, that also happens to be the number of elements in the table. However, if you use 0-based arrays then it won't count the zero-th element.
I have seen some difficult to debug issues caused by this when I used to work on game scripts.