table
Code Example
- definition of an empty table tabMyArray = {} Print (tabMyArray) - Table Assignment tabMyArray {= . 11 , 22 is , 33 is } for K, V in pairs (tabMyArray) do Print (V) End - definition of a "key-value pair "type table tabMyArray2 = {str1 = " lift Ares " , str2 = " fishing helmets " , Str3 = " I am the police " } - output two kinds Print (tabMyArray2.str1) Print (tabMyArray2 [ " str2"]) print(tabMyArray2["str3"]) for k, v in pairs(tabMyArray2) do print(tabMyArray2[k]) end
The basic structure and access to the table
- the key output of the iterative type table tabMyArray5 = {str1 = " elevator God of War " , str2 = " Fishing with a helmet " , str3 = " I am the police ." } - defined tables, direct assignment tabMyArray5.str4 = " your husband a word ah " tabMyArray5 [ " str5 " ] = " shame " - output for I = . 1 , . 5 do - using the # key iteration output table is not enough Print (tabMyArray5 [ " STR " ..i ]) End for k, v in pairs(tabMyArray5) do print(tabMyArray5[k]) end
Table Functions
- table function - to get the length of the table, 5.3 table.getn () function is not TAB1 = { " Hello " , " Goodbye " , " Good morning " } Print (# TAB1) - . 3 - Note: For table key-value pairs, the length can not be obtained with a # TAB2 NAME1 = = { " if things people " , NAME2 = " crown Xige " , NAME3 = " YJJ " } Print (TAB2 #) - 0 - table connection Print ( table.concat (tab1)) - hello goodbye good morning print( Table.concat (tab1, " | " )) - Hello | Goodbye | Good Morning Print ( table.concat (tab1, " | " , 2 , 3 )) - Goodbye | Good morning - removing table removal of the last is not specified TAB4 = { " Hello " , " JoJo " , " OK " } table.remove (TAB4) Print ( table.concat (TAB4, " | " )) - Hello | JoJo table.remove(tab4,2) print(table.concat(tab4," | ")) --hello | ok --表的排序 table.sort()函数 tab5 = {60,8,9,40,35} --数值 table.sort(tab5) print(table.concat(tab5," | ")) --8 | 9 | 35 | 40 | 60 tab6={"river","zone", " BREAK " , " Room " , " hello " } table.sort (tab6) Print ( table.concat (tab6, " | " )) - Room | Hello | BREAK | River | Zone - the largest table value table.maxn () function that there is a problem - the Lua 5.3 deprecated function tab7 = { . 1 , . 3 , 10 , . 5 , . 6 , . 7 , . 8 , . 9 } RES1 = table.maxn (tab7) Print (RES1) - output is the length of the table - the custom function selecting the maximum value function GeTabMaxNumber (Table) maxNum = 0 ; for K, V in pairs (Table) do IF (maxNum <Table [K]) the then maxNum = Table [K] End End return maxNum; End maxNum = GeTabMaxNumber (tab7) Print (maxNum)