cocos2d-x scripting language LUA basic data structure-tables (table)
Tableis aLuathe only data structure in the. Data structures provided by other languages, such as:Arrays,Records,lists,Queues,setsand so on. LuaIt's all through .Tableto achieve. And inLuainTableVery good implementation of these data structures.
--Excerpt from: "programming in Lua"
look at the code below to make it clear that the use of tables in Lua is very clear:
--table in Lua, tableconfig = {hello= "Hello Lua", world= "World"}--Assignment mode 1, initialize config.words = "Hello" as key = value--Assignment Mode 2, Assign a value Config.num = 100 by specifying the form of Table.key = value--Specifying the type config["name"] = "Zhangsan"-Assignment mode 3 by assigning a value. Assign a value by table["key"] = value-Prints the value of the table in the form print (config["words") print (config.name) print (Config.hello)-through the for iterator, Traverse table, Output table key and value for key, Var in pairs (Config) doprint (Key,var) End
The following code uses a table to define an array, and a table can also define matrices and multidimensional arrays, linked lists, queues, and bidirectional queues, collections, and packages.
--Define an array to initialize. Here the type of each element is initialized to be different arr = {1,2,3,4, "Hello"}--the iterated algebraic group for Key, Var in pairs (arr) doprint (Key,var) end--define an empty table arr = {}--through for loop, Call the Insert method of table, insert the value into the array for var=1, and Dotable.insert (Arr,1,var) end--iterate over the array. The print key, the value for key, the Var in pairs (arr) doprint (Key,var) end--calls the table's API. Gets the maximum value of the array in print (TABLE.MAXN (arr))
Reader, be able to go to the address below to see the API that LUA provides to our developers. Now the latest LUA version number is 5.2:HTTP://WWW.LUA.ORG/MANUAL/5.2/
Here you can see the use of the corresponding API, the meaning of the detailed parameters.
Cocos2d-x scripting language LUA basic data structure-tables (table)