Tables are the proprietary data structure in Lua. They represent arrays, lists, sets, records, graphs, an so on. A table in Lua is similar to an associative array. Associative arrays can be indexed with values of any type, not just numbers. Tables implement all these structures efficiently. For example, arrays can be implemented by indexing tables with integers. Arrays do not have a fixed size, but grow as needed. When initializing an array, its size is defined indirectly.
The following is an example of how tables can be constructed:
1 a = {} -- create a table with reference to "a" 2 b = "y" 3 a[b] = 10 -- new entry, with key="b" and value=10 4 a[20] = "Monday" -- new entry, with key=20 and value="Monday" 5 print(a["y"]) -- 10 6 b = 20 7 print(a[b]) -- "Monday" 8 c = "hello" -- new value assigned to "hello" property 9 print( c ) -- "hello"
You will notice in line 5 that a["y"]
is indexing the value from line 3. In line 7, a[b]
uses a new value of variable b
and...