<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > The Language and syntax > Expressions > Table Constructors |
Table constructors are expressions that create tables. Every time a constructor is evaluated, a new table is created. A constructor can be used to create an empty table or to create a table and initialize some of its fields. Below is an example of a table constructor:
mytable = {1,2,"10"}
Various methods can be used to initialize a table. In particular, you can specify a number of a table element that must be initialized. For example:
mytable = {10,20,[6]=30}
That code initializes: the table element 1 with 10, the table element 2 with 20, and the table element 6 with 30.
Below is more complicated example:
a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
That code is equivalent to the following:
do
local t = {}
t[f(1)] = g
t[1] = "x" -- 1st exp
t[2] = "y" -- 2nd exp
t.x = 1 -- t["x"] = 1
t[3] = f(x) -- 3rd exp
t[30] = 23
t[4] = 45 -- 4th exp
a = t
end
If the last field in the list has the form exp and the expression is a function call or a vararg expression, then all values returned by this expression enter the list consecutively (see Function Calls ). To avoid this, enclose the function call or the vararg expression in parentheses (see Expressions ).
The field list can have an optional trailing separator, as a convenience for machine-generated code.