Summary of common functions in the Table library of Lua

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

table is the Lua language An important data type, some characteristics of table are briefly listed as follows:
(1).table is an “associative array”, the index of the array can be a number or a string;
(2).table The default initial index generally starts with 1;
(3). The variable of the table is just an address reference, which will not affect the operation of the table;
(4). The table will not have a fixed length and size, and there will be new data The length will automatically grow when inserted;
(5). All index values ​​of the table need to be enclosed in “[“和”]”; if it is a string, you can also remove the quotation marks and brackets; that is, if there is no [] bracket It is considered to be a string index;
(6). All elements of the table are always separated by a comma “,”;

lua provides some auxiliary functions to manipulate the table, for example insert, remove, etc.
%—————————————————————————————————%
1、table .insert  and table.remove
table.insert inserts an element into the specified position:.
Example 1:

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

    The result of t will be {4, 1, 2, 3}< br>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
    }
    The second parameter of insert can be omitted , So that it will be inserted at the end of the array, eliminating the need to move other elements.
    Similarly, table.remove removes (and returns) an element from the array, table.remove(t,1) will remove the element whose subscript is 1 in t. If the removal position is not specified, then Remove the last one.
    %—————————————————————————————————%

    2, table.sort()
    Another useful function is sort, which sorts the array. If no sorting function is provided, the default is the Example 1:

    a = {1,3,2,6,4,8,7,5} print('Before sorting:',a) table.sort(a) print('After sorting :',a) span> 
    • 1
    • 2
    • 1
    • 2

    Result:
    Before sorting:
    {
    1: 1
    2: 3
    3: 2
    4: 6
    5: 4
    6: 8
    7: 7
    8: 5
    }
    After sorting:< br>{
    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< /span>
    • 1
    • 2
    • 3
    • < li>4

    • 5
    • 6
    • 1
    • 2< /li>
    • 3
    • 4
    • 5
    • 6

    The results are as follows:
    1 bill
    2 him
    3 me
    4 you
    %—————————————————————————————— ————-%

    3、table.concat()
    Format: table.concat(table, sep, start, end)
    concat() The function lists all the elements from the start position to the end position of the array part of the specified table in the parameter, and the elements are separated by the specified separator (sep). Except for table, no other parameters are required. The default value of the separator is a null character, the default value of start is 1, and the default value of end is the total length of the array part.
    Example:

    spring = {"Find him in the crowd Thousands of Baidu", "Suddenly Looking Back", "That People are in dim lights"} print( '一:',table.concat(spring, ",")) print('Two:',table.concat(spring, < span class="hljs-string">",",1,2)) print('Three:
    ',table.concat(spring, "
    ",1,3)) num = {1 ,2, 3,4,5 ,< span class="hljs-number">6} print('Size comparison:',table.concat (num ,"<")) span>  span>  span> 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    The results are as follows:
    One: Find in the crowd He looked back thousands of times, suddenly looking back, but the person was in the dim light
    Two: Looking for him thousands of Baidu in the crowd, suddenly looking back
    Three:
    Looking for him thousands of Baidu in the crowd
    Looking back suddenly
    That person Where the lights are dimmed
    Size comparison: 1<2<3<4<5<6
    %———————————————————————— ——————————-%

    4, table.maxn()
    Meaning: table.maxn() function returns all positive values ​​in the specified table The largest key value among the key values. If there is no element with a positive key value, 0 is returned. This function is not limited to the array part 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('The maximum key value is:',key)< /span>
    • 1
    • 2
    • 3
    • 4< /li>
    • 1
    • 2
    • 3
    • 4

    Result:
    {
    1: 0.2654
    2: 0.0109
    3: 0.3575< br>4: 0.8749
    5: 0.4324
    6: 0.1932
    }
    The maximum key value is: 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
    0
    %———— —————————————————————————————-%

    5、table.getn()
    Meaning: Return the number of elements in the table
    Example:

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

      Result: 4
      %—————————————————————————— ———————-%

      6、table.pack() and table.unpack()
      The table.pack function is to get an index starting from 1 The parameter table table, and a field n is predefined for this table, which indicates the length of the table.
      Example:

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

      Result:
      { 1: test
      2: param1
      3: param2
      4: param3
      n: 4
      }
      The table.unpack function is used to return the elements in the table, usage: table.unpack(table, start, end) , Where the parameter start is the position of the element to return, the default is 1, the parameter end is the position of the last element returned, and the default is the position of the last element of the table. The parameters start and end are optional.
      Example:

      t = {"Lua", "C++", "Python", "Java"} print('(1):
      ',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):',< span class="hljs-built_in">table.unpack(t, 2, 3)) span>  span>  span>
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

      The results are as follows:
      (1):
      {
      1: Lua
      2: C++
      3: Python
      4: Java
      }
      (2): Lua C++ python Java (3): C++ (4): Lua C++ Python Java (5): C++ Python Java (6): C++ Python

Leave a Comment

Your email address will not be published.