表库教程 |
|
手册简要介绍了该库的用途。我们在这里引用它:表库中的大多数函数都假设表表示数组或列表。对于这些函数,当我们谈论表的“长度”时,我们指的是长度运算符的结果 [即 #]。
将表的元素连接起来形成一个字符串。每个元素都必须能够被强制转换为字符串。可以指定一个分隔符,它放置在连接的元素之间。此外,可以在表中指定一个范围,从第 i 个元素开始,到第 j 个元素结束。
> = table.concat({ 1, 2, "three", 4, "five" })
12three4five
> = table.concat({ 1, 2, "three", 4, "five" }, ", ")
1, 2, three, 4, five
> = table.concat({ 1, 2, "three", 4, "five" }, ", ", 2)
2, three, 4, five
> = table.concat({ 1, 2, "three", 4, "five" }, ", ", 2, 4)
2, three, 4
concat 将在包含表的表上失败,因为它们不能被强制转换为字符串。有关强制转换的更多信息,请参阅 StringsTutorial。
> = table.concat({ 1,2,{} })
stdin:1: bad argument #1 to `concat' (table contains non-strings)
stack traceback:
[C]: in function `concat'
stdin:1: in main chunk
[C]: ?
(注意:此函数在 Lua 5.1 中已弃用,但它仍然可以用于打印表。您应该使用 pairs() 运算符。与 pairs() 一样,table.foreach() 方法不能保证按顺序返回索引键,这与这里的示例可能暗示的相反。)
将函数 f 应用于传递的表的元素。在每次迭代中,函数 f 会传递表中该元素的键值对。
> table.foreach({1,"two",3}, print) -- print the key-value pairs
1 1
2 two
3 3
> table.foreach({1,"two",3,"four"}, function(k,v) print(string.rep(v,k)) end)
1
twotwo
333
fourfourfourfour
如果函数 f 返回一个非nil 值,则迭代循环终止。
> table.foreach({1,"two",3}, function(k,v) print(k,v) return k<2 and nil end)
1 1
2 two
表可以包含混合的键值和索引值元素。table.foreach() 将显示表中的所有元素。要仅显示索引值元素,请参阅 table.foreachi()。有关此主题的更多信息,请参阅 TablesTutorial。
> t = { 1,2,"three"; pi=3.14159, banana="yellow" }
> table.foreach(t, print)
1 1
2 2
3 three
pi 3.14159
banana yellow
(注意:此函数在 Lua 5.1 中已弃用,但它仍然可以用于打印表。您应该使用 ipairs() 运算符。与 ipairs() 一样,table.foreachi() 方法保证按顺序返回索引键,并跳过非索引键。)
将函数 f 应用于传递的表的元素。在每次迭代中,函数 f 会传递表中该元素的索引值对。这类似于 table.foreach(),只是传递的是索引值对,而不是键值对。如果函数 f 返回一个非nil 值,则迭代循环终止。
> t = { 1,2,"three"; pi=3.14159, banana="yellow" }
> table.foreachi(t, print)
1 1
2 2
3 three
对表格的元素进行就地排序(即修改表格)。
> t = { 3,2,5,1,4 }
> table.sort(t)
> = table.concat(t, ", ") -- display sorted values
1, 2, 3, 4, 5
如果表格具有指定的大小,则只对指定范围进行排序,例如:
> t = { 3,2,5,1,4; n=3 } -- construct a table with user size of 3
> table.sort(t) -- sort will be limited by user size
> = table.concat(t, ", ") -- only specified size is concatenated as well
2, 3, 5
可以提供比较函数来自定义元素排序。比较函数必须返回一个布尔值,指定第一个参数是否应该在序列中位于第二个参数之前。默认行为是进行<比较。例如,以下行为与没有提供函数相同
> t = { 3,2,5,1,4 }
> table.sort(t, function(a,b) return a<b end)
> = table.concat(t, ", ")
1, 2, 3, 4, 5
我们可以看到,如果我们反转比较,序列顺序也会反转。
> table.sort(t, function(a,b) return a>b end) > = table.concat(t, ", ") 5, 4, 3, 2, 1
将给定值插入表格。如果给出了位置,则将值插入当前位于该位置的元素之前
> t = { 1,3,"four" }
> table.insert(t, 2, "two") -- insert "two" at position before element 2
> = table.concat(t, ", ")
1, two, 3, four
如果没有指定位置,我们将值追加到表格的末尾
> table.insert(t, 5) -- no position given so append to end > = table.concat(t, ", ") 1, two, 3, four, 5
当表格插入元素时,表格的大小和元素索引都会更新
> t = { 1,"two",3 } -- create a table
> = # t -- find current size
3
> table.foreach(t, print) -- display the table contents
1 1
2 two
3 3
> table.insert(t, 1, "inserted") -- insert an element at the start
> = table.concat(t, ", ") -- see what we have
inserted, 1, two, 3
> = # t -- find the size
4
> table.foreach(t, print) -- the indexes have been updated
1 inserted
2 1
3 two
4 3
当没有指定位置时,元素将根据计算的大小插入表格的末尾。表格的大小可能是用户指定的,并不反映元素的数量,例如:
> t = { 1,"two",3; n=10 } -- create a table with user size
> table.insert(t, "end") -- insert with no position inserts at "end"
> table.foreach(t, print) -- display the table contents
1 1
2 two
3 3
11 end
n 11
从表格中删除元素。如果指定了位置,则删除该位置的元素。剩余的元素将按顺序重新索引,表格的大小将更新以反映更改。此函数将返回删除的元素。例如:
> t = { 1,"two",3,"four" } -- create a table
> = # t -- find the size
4
> table.foreach(t, print) -- have a look at the elements
1 1
2 two
3 3
4 four
> = table.remove(t,2) -- remove element number 2 and display it
two
> table.foreach(t, print) -- display the updated table contents
1 1
2 3
3 four
> = # t -- find the size
3
如果没有给出位置,则删除表格中最后一个元素,该元素由表格的大小指定。例如:
> t = { 1,"two","three" }
> = # t -- find the table size (which is removed)
3
> table.foreach(t, print) -- display contents
1 1
2 two
3 three
> = table.remove(t) -- remove the element at position "n"
three
> table.foreach(t, print) -- display updated contents
1 1
2 two
> = # t -- display new size
2
如果表格的大小不反映元素的数量,则不会删除任何内容,例如:
> t = {1,2,3}
> table.setn(t,10) -- set user size
> table.foreach(t, print) -- display table contents, note size "n" is stored internally
1 1
2 2
3 3
> = # t -- find the size
10
> = table.remove(t) -- remove last element
nil
> = # t -- find the updated size
9
> table.foreach(t, print) -- display elements
1 1
2 2
3 3
请注意,table.remove 仅适用于数字索引。对于字典,您可以使用 tablevariable["index"] = nil; 来取消设置表格条目;