Lua table summarizes the common functions of the library

https://www.cnblogs.com/daochong/p/7363649.html

table is an important type of data Lua language, some of the characteristics of a simple table listed below: 
(. 1) is a .table "associative array" array index may be a number or a string; 
(2) a .table the default initial index generally begin 1; 
(. 3) .table variable is just a reference to the address, the operation of the table will not affect the data; 
(. 4) is not a fixed length .table size, there will automatically increase the length of the insertion of new data ; 
(. 5) .table all index values are needed "[" and "]" enclosed; If a string, and also removing quotes parentheses; i.e. if not enclosed in [], it is considered to string index; 
(6) .table among all the elements, always with a comma "," separated;

lua provide some helper functions to manipulate table, for example, insert, remove the like. 
------------------------------------%% 
1, table.insert and table.remove 
table.insert the element is inserted into a specified position: 
Example 1:

t = {1, 2, 3} table.insert(t, 1, 4}
  • 1
  • 2
  • 1
  • 2

the result will be t {4, 1, 2, 3} 
Example 2:

t={};
table.insert(t,"a");
table.insert(t,"b"); table.insert(t,"c");
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

The result is: 

. 1: A 
2: B 
. 3: C 

INSERT second parameter may be omitted, so that the end of the array will be inserted, so that without moving the other elements. 
Likewise, table.remove is removed (and back) of an element, table.remove (t, 1) t to remove the array element at index 1 from the array, if the removal position is not specified, then remove the last One. 
% ------------------------------------%

2, table.sort () 
Another useful function is Sort, to sort the array, if the sort function is not provided, the default is the <operation. Here not only the number but also the effective string. 
Example 1 :

a = {1,3,2,6,4,8,7,5} print('排序前:',a) table.sort(a) print('排序后:',a)
  • 1
  • 2
  • 1
  • 2

Results: 
front Sort: 

1: 1 
2: 3 
3: 2 
4: 6 
5: 4 
6: 8 
7: 7 
8: 5 

sorted: 

1: 1 
2: 2 
3: 3 
4: 4 
5: 5 
6: 6 
7: 7 
8: 8 

example 2:

name = {"you" ,"me", "him","bill" } --table.sort - only works with arrays! table.sort(name) for k, v in ipairs( name) do print( k,v) end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Results are as follows: 
. 1 Bill 
2 HIM 
. 3 Me 
. 4 you 
% ------------------------------------%

. 3, table.concat () 
Format: table.concat (table, On Sep, start, end) 
the concat () function specified in the parameters listed in all of the elements from the start position to the end position of the table portion of the array, to specify the separator between the elements character (On Sep) apart. In addition to the Table, the other parameters are not necessary, the default value is null character delimiter, the default value of start is 1, the default value of the total length of the array is the end portion. 
For example:

spring = {"众里寻他千百度", "蓦然回首", "那人却在灯火阑珊处"} print('一:',table.concat(spring, ",")) print('二:',table.concat(spring, ",",1,2)) print('三:\n',table.concat(spring, "\n",1,3)) num = {1 ,2, 3,4,5 ,6} print('大小比较:',table.concat (num ,"<"))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

The results are as follows: 
a: no trace of her thousands of Baidu, when I look back, that person is in the dim light 
two: her thousands of Baidu trace of her, I look back 
three: 
People look for him thousands of Baidu 
when I look back 
that person is in the dim light 
Compare Size: 1 <2 
<3 <4 <5 to <6 % ------------------------------------%

. 4, table.maxn () 
Meaning: table.maxn () function returns the number of all positive values in table key largest key value. If the element is a positive number key is not present, 0 is returned. This function is not limited to the array portion of the table. 
Example 1:

cc = { 0.2654,0.0109,  0.3575, 0.8749, 0.4324,0.1932} print(cc) key = table.maxn(cc) print('最大的key值为:',key)
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Results: 

1: 0.2654 
2: 0.0109 
3: 0.3575 
4: 0.8749 
5: 0.4324 
6: 0.1932 

The maximum value of key: 6 
Example 2:

apple = {"a" ,"p",[5]="e"} print(table.maxn(apple)) duck = {[-2]=3,[-1]=0} print(table.maxn(duck)) 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

The results are as follows: 
. 5 

% ------------------------------------%

. 5, table.getn () 
Meaning: Returns the number of elements in the table 
Example:

t1 = {1, 2, 3, 5} print(getn(t1))
  • 1
  • 2
  • 1
  • 2

Results: 4 
% ------------------------------------%

. 6, The Table.Pack () and table.unpack () 
The Table.Pack function is to obtain an index from a table starting parameter table, and the table will have a predefined field n, the length of the table. 
For example:

t = table.pack("test", "param1", "param2", "param3") print(t)
  • 1
  • 2
  • 1
  • 2

Results: 

. 1: Test 
2: the param1 
. 3: param2 
. 4: Param3 
n-:. 4 

table.unpack function returns the elements in the table, use: table.unpack (table, start, end ), wherein the parameter is started to return to start the position of the element, the default is 1, the parameters return the last element is the end position, the default is the last element of the Table, the parameter start, end are optional 
example:

t = {"Lua", "C++", "Python", "Java"} print('(1):\n',t) print('(2):',table.unpack(t)) a, b, c, d = table.unpack(t) print('(3):',b) print('(4):',a, b, c, d) print('(5):',table.unpack(t, 2)) print('(6):',table.unpack(t, 2, 3))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

The results are as follows: 
(. 1): 

. 1: the Lua 
2: C ++ 
. 3:  the Python 
. 4:  the Java 

(2): the Lua C ++  Python  the Java 
(. 3): C ++ 
(. 4): the Lua C ++ the Python the Java 
(5): C ++ Python the Java 
(6): C ++ Python

Guess you like

Origin www.cnblogs.com/nafio/p/11511225.html