Analysis on the Table function library in Lua

Source: Internet
Author: User

LuaAboutTableThe function library is the content to be introduced in this article, mainly to learnTableFunction library inLuaFor more information, see the detailed description. PartTableThe function only affects the array, while the other part affects the entireTableAre affected.

 
 
  1. table.concat(table, sep,  start, end) 

Concat is the abbreviation of concatenate (chained, connected. table. the concat () function lists all elements in the array part of the specified table in the parameter from the start position to the end position, and the elements are separated by the specified separator (sep. Except table, other parameters are not required. The default Delimiter is a null character, the default start value is 1, and the default end value is the total length of the array.

The sep, start, and end parameters are read in sequence. Therefore, although they are not required, to specify backend parameters, you must specify the preceding parameters.

 
 
  1. > tbl = {"alpha", "beta", "gamma"}  
  2. > print(table.concat(tbl, ":"))  
  3. alpha:beta:gamma  
  4. > print(table.concat(tbl, nil, 1, 2))  
  5. alphabeta  
  6. > print(table.concat(tbl, "\n", 2, 3))  
  7. beta  
  8. gamma  
  9. table.insert(table, pos, value)  

TableThe. insert () function specifies the position (pos) in the array part of the table to insert an element whose value is value. The pos parameter is optional. The default value is the end of the array part.

 
 
  1. > tbl = {"alpha", "beta", "gamma"}  
  2. > table.insert(tbl, "delta")  
  3. > table.insert(tbl, "epsilon")  
  4. > print(table.concat(tbl, ", ")  
  5. alpha, beta, gamma, delta, epsilon  
  6. > table.insert(tbl, 3, "zeta")  
  7. > print(table.concat(tbl, ", ")  
  8. alpha, beta, zeta, gamma, delta, epsilon  
  9. table.maxn(table)  

Table. the maxn () function returns the maximum value of all positive key values in the specified table. if no element with a positive key value exists, 0 is returned. this function is not limited to the array part of the table.

 
 
  1. > Tbl = {[1] = "a", [2] = "B", [3] = "c", [26] = "z "}
  2. > Print (# tbl)
  3. 3 -- because the 26 and the previous numbers are not consecutive, they are not included in the array.
  4. > Print (table. maxn (tbl ))
  5. 26
  6. > Tbl [91.32] = true
  7. > Print (table. maxn (tbl ))
  8. 91.32
  9. Table. remove (table, pos)

Table. The remove () function deletes and returns the elements in the pos position of the table array. the subsequent elements are moved forward. the pos parameter is optional. The default value is the table length, that is, the last element is deleted.

 
 
  1. table.sort(table, comp)  

The table. sort () function sorts the given table in ascending order.

 
 
  1. > tbl = {"alpha", "beta", "gamma", "delta"}  
  2. > table.sort(tbl)  
  3. > print(table.concat(tbl, ", "))  
  4. alpha, beta, delta, gamma 

Comp is an optional parameter. this parameter is an external function and can be used to customize the sorting standard of the sort function.

This function must meet the following conditions: accept two parameters (a and B in sequence) and return a Boolean value. When a should be placed before B, true is returned, otherwise, false is returned.

For example, when we need to sort in descending order, we can write as follows:

 
 
  1. > sortFunc = function(a, b) return b < a end  
  2. > table.sort(tbl, sortFunc)  
  3. > print(table.concat(tbl, ", "))  
  4. gamma, delta, beta, alpha 

Similar principles can also be used to write more complex sorting functions. For example, a table contains the names and levels of three members of a trade union:

 
 
  1. guild = {}  
  2.  
  3. table.insert(guild, {  
  4.  name = "Cladhaire",  
  5.  class = "Rogue",  
  6.  level = 70,  
  7. })  
  8.  
  9. table.insert(guild, {  
  10.  name = "Sagart",  
  11.  class = "Priest",  
  12.  level = 70,  
  13. })  
  14.  
  15. table.insert(guild, {  
  16.  name = "Mallaithe",  
  17.  class = "Warlock",  
  18.  level = 40,  
  19. }) 

When sorting this table, apply the following rules: sort by level in ascending order, and sort by name in ascending order at the same level.

You can write such a sorting function:

 
 
  1. function sortLevelNameAsc(a, b)  
  2.  if a.level == b.level then  
  3. return a.name < b.name 
  4.  else  
  5. return a.level < b.level 
  6.  end  
  7. end 

The test function is as follows:

 
 
  1. table.sort(guild, sortLevelNameAsc)  
  2.  for idx, value in ipairs(guild) do print(idx, value.name) end  
  3. 1, Mallaithe  
  4. 2, Cladhaire  
  5. 3, Sagart  
  6. table.foreachi(table, function(i, v)) 

A continuous Integer Range starting from 1 digit 1) is expected to traverse the key and value pairs in the table for function (I, v) operations.

 
 
  1. T1 = {2, 4, 6, language = "Lua", version = "5", 8, 10, 12, web = "hello lua "};
  2. Table. foreachi (t1, function (I, v) print (I, v) end); -- equivalent to foreachi (t1, print)

Output result:

 
 
  1. 1 2  
  2. 2 4  
  3. 3 6  
  4. 4 8  
  5. 5 10  
  6. 6 12  
  7. table.foreach(table, function(i, v)) 

Different from foreachi, foreach performs iteration on the entire table.

T1 = {2, 4, 6, language = "Lua", version = "5", 8, 10, 12, web = "hello lua "};
Table. foreach (t1, function (I, v) print (I, v) end );

Output result:

 
 
  1. 1 2  
  2. 2 4  
  3. 3 6  
  4. 4 8  
  5. 5 10  
  6. 6 12  
  7. web hello lua  
  8. language Lua  
  9. version 5  
  10. table.getn(table) 

Returns the number of elements in the table.

 
 
  1. t1 = {1, 2, 3, 5};  
  2. print(getn(t1))  
  3. ->4  
  4. table.setn(table, nSize) 

SetTableNumber of elements in.

Summary: AnalysisLuaAboutTableThe function library has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.