An array is a contiguous chunk of memory; some programming languages guarantee that the memory will be contiguous. In Lua, an array might use a linear chunk of memory if the following apply:
- The table has only numeric indices
- The numeric indices start from one
- At least half of the indices are not nil
How tables are implemented is dependent on the internals of Lua. This is not something you usually have to concern yourself with, but if you are interested, the topic is described in detail at http://www.lua.org/doc/jucs05.pdf.
For now, assume that an array is defined as a table that is indexed only numerically, starting with index 1. By this definition, the following code demonstrates how to use a table as an array:
arr = {}
arr[1] = "x"
arr[2] = "y"
arr[3] = "z"
for i = 1,3 do
print(arr[i])
end