在 Lua 中,表(Tables)是唯一可用的数据结构,可帮助我们创建不同类型的数组和字典。Lua 使用关联数组,不仅可以用数字索引,还可以用非nil
的字符串索引。表大小不固定,可以根据需要动态增长。
Lua 在所有的表示中都使用表,包括用表表示包。当访问 string.format 方法时,实际上是在访问字符串包中的 format 函数。
表被称为对象,它们既不是值也不是变量。Lua 使用构造表达式 {}
来创建空表。需要知道的是,变量持有表的引用与表本身之间没有固定的关系。
-- 示例表的初始化
mytable = {
}
-- 简单的表值赋值
mytable[1] = "Lua"
-- 移除引用
mytable = nil
-- Lua 的垃圾回收机制会释放内存
当表 a
具有一组元素并将其赋值给 b
时,a
和 b
都引用相同的内存。对 b
不会单独分配内存。当 a
被设置为nil
时,表仍然可以被 b
访问。当没有引用指向表时,Lua 的垃圾回收机制会清理这些没有引用的内存,以便再次使用。
下面是一个示例,用于解释上述表的特点:
-- Simple empty table
mytable = {}
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 table
alternatetable = mytable
print(”alternatetable Element at index 1 is “, alternatetable[1])
print(”alternatetable 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 table
alternatetable = nil
print(”alternatetable is “, alternatetable)
-- mytable is still accessible
print(”mytable Element at index wow is “, mytable[”wow“])
mytable = nil
print(”mytable is “, mytable)
运行上述程序时,将得到以下输出:
Type of mytable is table
mytable Element at index 1 is Lua
mytable Element at index wow is Tutorial
alternatetable Element at index 1 is Lua
alternatetable Element at index wow is Tutorial
mytable Element at index wow is I changed it
alternatetable is nil
mytable Element at index wow is I changed it
mytable is nil
Lua有一些内置函数用于表操作,如下列表:
序号 | 方法和用途 |
---|---|
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"}
-- 返回表的连接字符串
print("Concatenated string ", table.concat(fruits))
-- 用字符连接
print("Concatenated string ", table.concat(fruits, ", "))
-- 基于索引连接水果
print("Concatenated string ", table.concat(fruits, ", ", 2, 3))
运行上述程序时,会得到以下输出:
Concatenated string bananaorangeapple
Concatenated string banana, orange, apple
Concatenated string orange, apple
在表中插入和删除项是最常见的操作。如下代码所示:
fruits = {
"banana", "orange", "apple"}
-- 在末尾插入水果
table.insert(fruits, "mango")
print("Fruit at index 4 is ", fruits[4])
-- 在索引 2 处插入水果
table.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 mango
Fruit at index 2 is grapes
The maximum elements in table is 5
The last element is mango
The previous last element is nil
我们经常需要按特定顺序对表进行排序。sort 函数根据字母表顺序对表中的元素进行排序。示例如下 :
fruits = {
"banana", "orange", "apple", "grapes"}
for k, v in ipairs(fruits) do
print(k, v)
end
table.sort(fruits)
print("sorted table")
for k, v in ipairs(fruits) do
print(k, v)
end
运行上述程序时,会得到以下输出:
1 banana
2 orange
3 apple
4 grapes
sorted table
1 apple
2 banana
3 grapes
4 orange
文章浏览阅读7.2k次。前段时间开发手持机上的软件,因为A8手持机的射频卡可存储的内容太小,并且需要存储16进制数据,因此就写了一个工具类。上代码:package cn.com.szh;import java.io.UnsupportedEncodingException;public class Main { public static void main(String[] args) { Stri..._bytearraytohexstring
文章浏览阅读4.9k次。#include #include using namespace std;using namespace cv;int main(){Mat src; //源图像Mat tmp; //临时图像Mat dst_bw; //去掉背景后的目标二值图像Mat dst_contours;//轮廓图像src=imread("E:\\单板图片\\求孔洞数_边缘的最小外接矩形
文章浏览阅读865次。中介者,说白了跟市面上黑中介类似。当然这个中介,开发者是可以控制其行为的。也是在一定的信任关系上建立的。该模式要解决的问题是,一堆对象之间交叉耦合问题。网上看过群聊的例子。如果没有任何一个平台,多人之间的会话会是什么样的呢?不举多人,就三个吧A想把一句话说给BC,那么他首先要知道B和C在哪儿,然后分别告诉对方,自己想说的事情。如果再加一个人呢?问题很明显,此时各种群聊工具应运而生。我写
文章浏览阅读1.8k次。AUTO_INCREMENT两种情况1、在载入语句执行前,已经不确定要插入多少条记录。在执行插入语句时在表级别加一个auto-inc锁,然后为每条待插入记录的auto-increment修饰的列分配递增的值,语句执行结束后,再把auto-inc锁释放掉。一个事务再持有auto-inc锁的过程中,其他事务的插入语句都要被阻塞,可以保证一个语句中分配的递增值是连续的。AUTO-INC锁的..._mysql 自增序列生成原理
文章浏览阅读3.5k次,点赞2次,收藏17次。半导体能带结构示意图:上方两条白色带为没有电子填充的带,下面三条灰色带为充满电子的带,其中最高一条灰色带为价带,它与最低一条白色带之间的空隙为能隙空穴又称电洞(Electron hole),在固体物理学中指共价键上流失一个电子,最后在共价键上留下空位的现象导带(英语:conduction band),又名传导带,是指半导体或是绝缘体材料中,一种电子所具有能量的范围。这个能量的范围高..._掺杂半导体的带隙
文章浏览阅读3.5k次,点赞2次,收藏26次。基于C++和OpenCV的中心线提取算法加权平方灰度重心法介绍算法演示加权平方灰度重心法介绍详情见 https://blog.csdn.net/u010518385/article/details/101015604算法演示下面展示 函数-输入图像和阈值,输出点。void get_median_line(Mat& src, int thresh, vector<Point2d>& points){ if (src.empty()) return; // 一、_图像中心线提取c++
文章浏览阅读1.5w次,点赞15次,收藏74次。Socket1 环境查看通过cmd窗口的命令:ipconfig查看本机IP地址查看网络情况是否正常:ping百度官网用来进行本地测试的地址 127.0.0.1,回环测试地址,默认代表的就是本机的IP2 Socket概述socket编程也叫套接字编程,应用程序可以通过它发送或者接受数据,可对其像打开文件一样打开/关闭/读写等操作.套接字允许应用程序将I/O插入到网络中,并与网络中的其他应用程序进行通信.网络套接字是IP地址与端口号TCP协议的组合Socket就是为网络编程提供的一_socket网络编程
文章浏览阅读574次。I installed the mp3spi to support reading mp3 files in my Java 8 project usng the javax.sound* libraries. My goal now is to write mp3 to a wav file. However, the result is incorrect. Here's the code i..._java mp3转wav
文章浏览阅读2.7w次,点赞4次,收藏18次。正好要做一个天空的场景,想添加上行星和恒星的自转和公转,代码如下1.自转。public float _RotationSpeed; //定义自转的速度transform.Rotate(Vector3.down*_RotationSpeed,Space.World); //物体自转2.公转 public GameObject Axis; //物体需要公转的参_unity2d 公转
文章浏览阅读7.6k次。 很多其他语言的libary都会有去除string类的首尾空格的库函数,但是标准C++的库却不提供这个功能。但是C++string也提供很强大的功能,实现trim这种功能也不难。下面是几种方法: 1.使用string的find_first_not_of,和find_last_not_of方法 /* Filename : StringTrim1...._c++ string trim
文章浏览阅读4.4k次,点赞4次,收藏26次。现在的java后端基本都是通过微服务的方式进行搭建。当我们对需求进行分割时,可以通过横向或者纵向对服务进行划分。或者当某一块的业务我们希望通过一个单独的服务进行开发时,就需要新增新的服务,本文通过springtool suite工具介绍,微服务搭建过程。_java 微服务
文章浏览阅读94次。[code="sql"]1. 概要说明数据库系统的显著特点需要保存大量历史记录,系统内存在许多历史记录表,因此常常出现系统运行一段时间,表记录数达到一定数量后,系统响应明显变慢的现象。为避免这种情况的出现,在有完备的数据库对象设计下,还在考虑到系统稳定情况下数据的变化情况,针对这种变化,在编写SQL语句必须遵循一定的优化规则,并制定完备的数据管理机制。2. 调优目的利用Or..._sql性能调优,一般读多少次算大