In Lua all arrays are just dictionaries with integer keys, a[0] will work just fine. It’s just that all built-in functions will expect arrays that start with index 1.
PHP did that same thing. It was a big problem when algorithmic complexity attacks were discovered. It took PHP years to integrate an effective solution that didn’t break everything.
That’s slightly misleading, I think. There are no arrays in Lua, every Lua data structure is a table (sometimes pretending to be something else) and you can have anything as a key as long as it’s not nil. There’s also no integers, Lua only has a single number type which is floating point. This is perfectly valid:
local tbl = {}
local f = function()error(":(") end
tbl[tbl] = tbl
tbl[f] = tbl
tbl["tbl"] = tbl
print(tbl)
-- table: 0x557a907f0f40print(tbl[tbl], tbl[f], tbl["tbl"])
-- table: 0x557a907f0f40 table: 0x557a907f0f40 table: 0x557a907f0f40for key,value inpairs(tbl) doprint(key, "=", value)
end-- tbl = table: 0x557a907f0f40-- function: 0x557a907edff0 = table: 0x557a907f0f40-- table: 0x557a907f0f40 = table: 0x557a907f0f40print(type(1), type(-0.5), type(math.pi), type(math.maxinteger))
-- number number number number
In Lua all arrays are just dictionaries with integer keys, a[0] will work just fine. It’s just that all built-in functions will expect arrays that start with index 1.
PHP did that same thing. It was a big problem when algorithmic complexity attacks were discovered. It took PHP years to integrate an effective solution that didn’t break everything.
Your argument isn’t making me any happier - it just fills me with more rage.
That’s slightly misleading, I think. There are no arrays in Lua, every Lua data structure is a table (sometimes pretending to be something else) and you can have anything as a key as long as it’s not nil. There’s also no integers, Lua only has a single number type which is floating point. This is perfectly valid:
local tbl = {} local f = function() error(":(") end tbl[tbl] = tbl tbl[f] = tbl tbl["tbl"] = tbl print(tbl) -- table: 0x557a907f0f40 print(tbl[tbl], tbl[f], tbl["tbl"]) -- table: 0x557a907f0f40 table: 0x557a907f0f40 table: 0x557a907f0f40 for key,value in pairs(tbl) do print(key, "=", value) end -- tbl = table: 0x557a907f0f40 -- function: 0x557a907edff0 = table: 0x557a907f0f40 -- table: 0x557a907f0f40 = table: 0x557a907f0f40 print(type(1), type(-0.5), type(math.pi), type(math.maxinteger)) -- number number number number