Lua Study Notes --- tables (arrays), lua Study Notes --- Arrays
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio> T = {}> T[0] = 0> T[1] = 1> for i = 0,1,1 do print(T[i]) end01>
The subscript can be a string,
> T ["h1"] ={}> T ["h1"] ["name"] = "jarlen"> T ["h1"] ["age"] = 26> print (T ["h1"]) table: 003BD180 -- cannot be referenced directly. This is the address of T ["h1"]> print (T ["h1"] ["name"]). jarlen> print (T ["h1"] ["age"]) 26 when the subscript index is a string, you can also write the following:> print (T. h1.name) jarlen> print (T. h1.age) 26
> T.h1.age = 28> print(T.h1.age)28> T.h1.name = "ja"> print(T.h1.name)ja
2> You can also create and initialize data.
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio> T = {1,2}> print(T[1])1> print(T[0])nil> print(T[2])2>
It can be seen that T starts from "1" except for the string subscript.
> T2 = {name = "T2",age = 2}> print(T2.name)T2> print(T2.age)2
For convenience, we can write all the data in the table when defining the table.
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio> T = {>> 14,>> jarlen = {name = "jarlen",age = 26},>> name = "haha"}> print(T.jarlen.name)jarlen> print(T[1])14> print(T["name"])haha> print(T["jarlen"]["name"])jarlen> print(T["jarlen"]["age"])26>
Another example:
> T3 = {>> 14,>> ["jarlen"] = {["name"] = "jarlen",["age"] = 26},>> ["name"] = "haha",>> 20}> print(T3[1])14> print(T3[2])20> print(T3.jarlen.name)jarlen> print(T3.jarlen.age)26
Note:
1> all elements must be separated by commas;
2> the element of an element digit. The subscript starts from the first digit element and is "1". The subsequent digits are arranged in this order.
3> All index values must be enclosed by "[" and "]". If the index value is a string, you can remove the quotation marks and brackets.
It looks like a configuration file and is easy to understand.