July 2018
Beginner
202 pages
5h 42m
English
Lua has one-based arrays. This means the language assumes that the first element of the array will occupy index 1. This is in contrast to languages such as C or Java, in which arrays start at index 0. The default array constructor places the first element of the array in index 1, as follows:
vector = { "x", "y", "z" }print (tostring(vector[0])) -- nil, the array starts at 1print (vector[1]) -- first element, xprint (vector[2]) -- second element, yprint (vector[3]) -- third element, z
Lua is a very forgiving language. You can absolutely assign any value to any index. This means, if you really want to, you can explicitly place a value in index 0 as follows:
vector = { [0] = "x", "y", "z", "w" }print (vector[0]) -- element ...Read now
Unlock full access