The constructor is the expression used to create and initialize tables. A table is unique to Lua. The simplest constructor is {}, which is used to create an empty table. You can directly initialize the array:
1 days = {"Sunday", "Monday", "Tuesday", "Wednesday",2 "Thursday", "Friday", "Saturday"}
Lua initializes "Sunday" to days [1] (the first element index is 1), and uses "Monday" to initialize days [2]...
1 print(days[4]) --> Wednesday
Constructors can use any expression to initialize:
1 tab = {sin(1), sin(2), sin(3), sin(4),2 sin(5),sin(6), sin(7), sin(8)}
To initialize a table as a record, you can:
1 a = {x=0, y=0} <--> a = {}; a.x=0; a.y=0
No matter how you create a table, you can add or delete any type of fields to the table. The constructor only affects table initialization.
1 w = {x=0, y=0, label="console"}2 x = {sin(0), sin(1), sin(2)}3 w[1] = "another field"4 x.f = w5 print(w["x"]) --> 06 print(w[1]) --> another field7 print(x.f[1]) --> another field8 w.x = nil -- remove field "x"
Every time you call the constructor, Lua creates a new table. You can use the table to construct a list:
1 list = nil2 for line in io.lines() do3 list = {next=list, value=line}4 end
This Code reads each line from the standard input, and then forms a linked list in reverse sequence. The following code prints the content of the linked list:
1 l = list2 while l do3 print(l.value)4 l = l.next5 end
In the same constructor, you can mix the list style and record style for initialization, for example:
1 polyline = {color="blue", thickness=2, npoints=4,2 {x=0, y=0},3 {x=-10, y=0},4 {x=-10, y=1},5 {x=0, y=1}6 }
This example also shows that we can nested constructors to represent complex data structures.
There are restrictions on the initialization methods of the above two constructors. For example, you cannot use a negative index to initialize elements in a table, and the string index cannot be properly represented. The following describes a more general initialization method. We use the [expression] to display the index to be initialized:
1 opnames = {["+"] = "add", ["-"] = "sub",2 ["*"] = "mul", ["/"] = "div"}3 4 i = 20; s = "-"5 a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s}6 7 print(opnames[s]) --> sub8 print(a[22]) --> ---
List-style initialization and record-style initialization are special cases of such general initialization:
1 {x=0, y=0} <--> {["x"]=0, ["y"]=0}2 {"red", "green", "blue"} <--> {[1]="red", [2]="green", [3]="blue"}
If you really want the array subscript to start from 0:
1 days = {[0]="Sunday", "Monday", "Tuesday", "Wednesday",2 "Thursday", "Friday", "Saturday"}
Note: It is not recommended that the array subscript start from 0, otherwise many standard libraries cannot be used.
The "," at the end of the constructor is optional and can be easily expanded in the future.
1 a = {[1]="red", [2]="green", [3]="blue",}
In the constructor, the field separator comma (",") can be replaced by a semicolon (";"). Generally, we use a semicolon to separate different types of table elements:
1 {x=10, y=45; "one", "two", "three"}