简介
表是Lua中唯一可用的数据结构,可帮助我们创建不同的类型,如数组和字典. Lua使用关联数组,不仅可以使用数字编制索引,还可以使用除nil之外的字符串编制索引.表没有固定的大小,可以根据我们的需要增长.
Lua在所有表示中使用表,包括包的表示.当我们访问方法string.format时,这意味着,我们正在访问字符串包中可用的格式函数.
表示和用法
表被称为对象,它们既不是值也不是变量. Lua使用构造函数表达式{}来创建一个空表.众所周知,保存表的引用的变量与表本身之间没有固定的关系.
--sample table initializationmytable = {}--simple table value assignmentmytable[1]= "Lua"--removing referencemytable = nil-- lua garbage collection will take care of releasing memory
当我们有一个表 a 有一组元素,如果我们将它分配给 b , a 和 b 都指的是同一个内存.没有单独为b分配单独的内存.当a设置为nil时,b仍然可以访问表.当没有对表的引用时,Lua中的垃圾收集负责清理进程以使这些未引用的内存再次被重用.
下面显示了一个示例来解释上述内容提到的表格功能.
-- Simple empty tablemytable = {}print("Type of mytable is ",type(mytable))mytable[1]= "Lua"mytable["wow"] = "Tutorial"print("mytable Element at index 1 is ", mytable[1])print("mytable Element at index wow is ", mytable["wow"])-- alternatetable and mytable refers to same tablealternatetable = mytableprint("alternatetable Element at index 1 is ", alternatetable[1])print("mytable Element at index wow is ", alternatetable["wow"])alternatetable["wow"] = "I changed it"print("mytable Element at index wow is ", mytable["wow"])-- only variable released and and not tablealternatetable = nilprint("alternatetable is ", alternatetable)-- mytable is still accessibleprint("mytable Element at index wow is ", mytable["wow"])mytable = nilprint("mytable is ", mytable)
当我们运行上述程序时我们将得到以下输出 :
Type of mytable is tablemytable Element at index 1 is Luamytable Element at index wow is Tutorialalternatetable Element at index 1 is Luamytable Element at index wow is Tutorialmytable Element at index wow is I changed italternatetable is nilmytable Element at index wow is I changed itmytable is nil
表操作
那里是用于表操作的内置函数,它们列在下表.
Sr.No . | 方法&目的 |
---|---|
1 | table.concat(table [,sep [,i [,j]]]) 根据给定的参数连接表中的字符串.详见示例. |
2 | table.insert(table,[pos,] value) 在指定位置的表格中插入一个值. |
3 | table. maxn(table) 返回最大的数字索引. |
4 | table.remove(table [,pos]) 从表中删除值. |
5 | table.sort(table [,comp]) 根据可选的比较器参数对表进行排序. |
让我们看一些上述函数的样本.
表连接
我们可以使用concat函数连接两个表,如下所示 :
fruits = {"banana","orange","apple"}-- returns concatenated string of tableprint("Concatenated string ",table.concat(fruits))--concatenate with a characterprint("Concatenated string ",table.concat(fruits,", "))--concatenate fruits based on indexprint("Concatenated string ",table.concat(fruits,", ", 2,3))
当我们运行上述程序时,我们将得到以下输出 :
Concatenated string bananaorangeappleConcatenated string banana, orange, appleConcatenated string orange, apple
插入和删除
在表格操作中,最常见的是在表格中插入和删除项目.它解释如下.
fruits = {"banana","orange","apple"}-- insert a fruit at the endtable.insert(fruits,"mango")print("Fruit at index 4 is ",fruits[4])--insert fruit at index 2table.insert(fruits,2,"grapes")print("Fruit at index 2 is ",fruits[2])print("The maximum elements in table is",table.maxn(fruits))print("The last element is",fruits[5])table.remove(fruits)print("The previous last element is",fruits[5])
当我们运行上面的程序,我们将得到以下输出 :
Fruit at index 4 is mangoFruit at index 2 is grapesThe maximum elements in table is5The last element ismangoThe previous last element isnil
排序表
我们经常需要按特定顺序对表进行排序.排序函数按字母顺序对表中的元素进行排序.此示例如下所示.
fruits = {"banana","orange","apple","grapes"}for k,v in ipairs(fruits) do print(k,v)endtable.sort(fruits)print("sorted table")for k,v in ipairs(fruits) do print(k,v)end
当我们运行上述程序时,我们将得到以下输出 :
1banana2orange3apple4grapessorted table1apple2banana3grapes4orange