Initializing Tables
Initializing Tables
There are two basic ways in which table elements can be initialized. The first assigns expressions in the list to consecutive numerical indices, starting with an index value of 1:
VAR = { EXP { , EXP} [ , ] }
The second form lists key-value pairs:
VAR = { NAME = EXP {, NAME = EXP} [ , ] }
where NAME = EXP is syntactic sugar for [EXP] = EXP
In both forms, the final trailing comma is always optional. In addition, one method of construction may follow the other, separated by a semicolon. For example, all forms below are correct:
x = {;}
x = {"a", "b",}
x = {type="list"; "a", "b"}
x = {f(0), f(1), f(2),; n=3,}
The field “n” in a table is used in many functions as a “size” indicator, otherwise “size” is the largest numerical index with a non-nil value.Where possible, use functions like getn, foreachi , tinsert and tremove instead of modifying field “n” directly.