引言:

最近刚稍微深入了解一下Lua,正好最近需要用到Lua中对表的操作,于是借助现有的了解实现了对一个简单的二维表进行添加、修改、计算、删除及判断存在的操作

表的创建及相关方法:

1. 创建表及自定义一个迭代器:

--1.当作这是一个操作类/表;该表为一个二维列表,每一个元素代表一个表
--2.每一个元素(表)中包含 id 和 num
operationList = {} --1.列表元素迭代器,仅返回列表中每一个元素,改列表索引必须为连续的数字
function listIterator(list)
local index = 0;
local listLen = #list
return function ()
index = index + 1
if index <= listLen then
return list[index]
end
end
end

2. 向表中添加数据

--1.向操作类/表中添加数据,oId代表标识ID,onum代表值当前值
--2.当 oId 已在列表中存在时,不进行添加操作
--2.当 oId 为nil时,默认为 “1”,当 onum 为nil时,默认为 1
--3.oId 类型为string,oNum 类型为number
--4.添加成功返回 true,失败则返回 false
function operationList:add(oId, oNum)
if not operationList:exist(oId) then
table.insert(self, {id = oId or "1", num = oNum or 1})
return true
end
return false
end

3. 修改表中数据

--1.修改operationList指定 ID 的值
--2.oId为指定ID,oNum为修改值,ifNum为指定ID的指定值,oCreate为是否新建值
--2.oId 类型为string,oNum 类型为number,ifNum类型为number,oCreate类型为boolean
--3.当该 ID 不存在时且参数 oCreate = true,则创建
--4.若当前 ID 存在, oNum 为 nil,ifNum 不存在,返回当前 ID 的 num 值
--5.若当前 ID 存在, oNum 不为 nil,ifNum 不存在,则为修改后的 num 值
--6.若当前 ID 存在, oNum 不为 nil,ifNum 存在,则为修改后的 num 值
--7.若当前 ID 存在, oNum 不为 nil,ifNum 不存在,返回当前 ID 的 num 值
--8.若当前 ID 不存在,且 oCreate = false 或 = nil,则返回数字 -1
--9.若当前 ID 不存在,且 ocreate = true,则创建对应 ID 和 num = 1 的新子列,再返回数字 0
function operationList:alter(oId, oNum, ifNum, oCreate)
if not ifNum then
for ol in listIterator(operationList) do
if ol.id == oId then --若与要修改的ID相同
if oNum then --若修改值不为nil
ol.num = oNum --修改值
end
return ol.num --返回当前值,若无修改值,则返回原来的值
end
end
else
for ol in listIterator(operationList) do
if ol.id == oId then --若与要修改的ID相同且 该ID的值 = 判定值
if ol.num == ifNum then
ol.num = oNum --修改值
end
return ol.num --返回当前值,若原来的值不等于ifNum的值,则返回原来的值
end
end
end
if not oCreate then --若列表不存在 oId,且 oCreate的值为false
return -1 --返回 -1
end
operationList:add(oId, 1) --若列表不存在 oId,且 oCreate的值为true
return 0 --返回 0
end

3.1. 修改表略微修改版

--1.修改operationList指定 ID 的值
--2.oId为指定ID,oNum为修改值,ifNum为指定ID的指定值,oCreate为是否新建值
--2.oId 类型为string,oNum 类型为number,ifNum类型为number,oCreate类型为boolean
--3.当该 ID 不存在时且参数 oCreate = true,则创建
--4.若当前 ID 存在, oNum 为 nil,ifNum 不存在,返回当前 ID 的 num 值
--5.若当前 ID 存在, oNum 不为 nil,ifNum 不存在,则为修改后的 num 值
--6.若当前 ID 存在, oNum 不为 nil,ifNum 存在,则为修改后的 num 值
--7.若当前 ID 存在, oNum 不为 nil,ifNum 不存在,则返回 -2
--8.若当前 ID 不存在,且 oCreate = false 或 = nil,则返回数字 -1
--9.若当前 ID 不存在,且 ocreate = true,则创建对应 ID 和 num = 1 的新子列,再返回数字 0
function operationList:alter(oId, oNum, ifNum, oCreate)
if not ifNum then
for ol in listIterator(operationList) do
if ol.id == oId then --若与要修改的ID相同
if oNum then --若修改值不为nil
ol.num = oNum --修改值
end
return ol.num --返回当前值,若无修改值,则返回原来的值
end
end
else
for ol in listIterator(operationList) do
if ol.id == oId then --若与要修改的ID相同且 该ID的值 = 判定值
if ol.num == ifNum then
ol.num = oNum --修改值
return ol.num --返回修改后的值
end
return -2
end
end
end
if not oCreate then --若列表不存在 oId,且 oCreate的值为false
return -1 --返回 -1
end
operationList:add(oId, 1) --若列表不存在 oId,且 oCreate的值为true
return 0 --返回 0
end

4. 计算表中值数据

--1.计算operationList指定 ID 的值
--2.oId为指定ID,computeNum为计算值,ifNum为指定ID的指定值,oCreate为是否新建值
--2.oId 类型为string,computeNum 类型为number,ifNum类型为number,oCreate类型为boolean
--3.当该 ID 不存在时且参数 oCreate = true,则创建
--4.若当前 ID 存在, computeNum 为 nil,ifNum 不存在,返回当前 ID 的 num + 1 值
--5.若当前 ID 存在, computeNum 不为 nil,ifNum 不存在,则为修改后的 num + computeNum 值
--6.若当前 ID 存在, computeNum 不为 nil,ifNum 存在,则为修改后的 num + computeNum 值
--7.若当前 ID 存在, computeNum 不为 nil,ifNum 不存在,则返回当前 ID 的 num 值
--8.若当前 ID 不存在,且 oCreate = false 或 = nil,则返回数字 -1
--9.若当前 ID 不存在,且 ocreate = true,则创建对应 ID 和 num = 1 的新子列,再返回数字 0
function operationList:compute(oId, computeNum, ifNum, oCreate)
if not ifNum then --如果相等判定值不存在
for ol in listIterator(operationList) do
if ol.id == oId then
if not computeNum then --如果计算值为空,那么原值默认 + 1
ol.num = ol.num + 1
else
ol.num = ol.num + computeNum --计算值不为空,那么原值 + 计算值
end
return ol.num
end
end
else
for ol in listIterator(operationList) do
if ol.id == oId then
if ol.num == ifNum and computeNum then
ol.num = ol.num + computeNum
end
return ol.num
end
end
end
if not oCreate then --若列表不存在 oId,且 oCreate的值为false
return -1 --返回 -1
end
operationList:add(oId, 1) --若列表不存在 oId,且 oCreate的值为true
return 0
end

4.1. 计算表略微修改版

--1.计算operationList指定 ID 的值
--2.oId为指定ID,computeNum为计算值,ifNum为指定ID的指定值,oCreate为是否新建值
--2.oId 类型为string,computeNum 类型为number,ifNum类型为number,oCreate类型为boolean
--3.当该 ID 不存在时且参数 oCreate = true,则创建
--4.若当前 ID 存在, computeNum 为 nil,ifNum 不存在,返回当前 ID 的 num + 1 值
--5.若当前 ID 存在, computeNum 不为 nil,ifNum 不存在,则为修改后的 num + computeNum 值
--6.若当前 ID 存在, computeNum 不为 nil,ifNum 存在,则为修改后的 num + computeNum 值
--7.若当前 ID 存在, computeNum 不为 nil,ifNum 不存在,则返回 -2
--8.若当前 ID 不存在,且 oCreate = false 或 = nil,则返回数字 -1
--9.若当前 ID 不存在,且 ocreate = true,则创建对应 ID 和 num = 1 的新子列,再返回数字 0
function operationList:compute(oId, computeNum, ifNum, oCreate)
if not ifNum then --如果相等判定值不存在
for ol in listIterator(operationList) do
if ol.id == oId then
if not computeNum then --如果计算值为空,那么原值默认 + 1
ol.num = ol.num + 1
else
ol.num = ol.num + computeNum --计算值不为空,那么原值 + 计算值
end
return ol.num
end
end
else
for ol in listIterator(operationList) do
if ol.id == oId then
if ol.num == ifNum and computeNum then
ol.num = ol.num + computeNum
return ol.num
end
return -2
end
end
end
if not oCreate then --若列表不存在 oId,且 oCreate的值为false
return -1 --返回 -1
end
operationList:add(oId, 1) --若列表不存在 oId,且 oCreate的值为true
return 0
end

5. 删除指定ID的数据

--1.从表移除指定 ID 的子列
--2.oId 类型为string
--3.移除成功返回 true,失败返回 false(只有当oId不存在时才会失败)
function operationList:remove(oId)
local removeIndex
for olI, olV in pairs(operationList) do
if olV.id == oId then --如果找到ID,则将对应索引传递给removeIndex
removeIndex = olI
break
end
end
if not removeIndex then
return false
end
table.remove(operationList, removeIndex)
return true
end

6. 判断指定ID是否存在表中

--1.判断指定 ID 在operationList中是否存在
--2.oId 类型为string
function operationList:exist(oId)
for ol in listIterator(operationList) do
if ol.id == oId then
return true
end
end
return false
end

对以上方法进行测试:

1. 测试代码:

--下面是向表operation中插入数据及输出结果
operationList:add("test1", 100)
operationList:add("test2", 200)
operationList:add("test3", 300)
operationList:add("test4")
operationList:add(nil, 500) --下面是操作表
operationList:remove("test3") --移除数据
print(operationList:exist("11")) --判断 ID = 11 的子列是否存在,输出false
print(operationList:exist("1")) --判断 ID = 1 的子列是否存在,输出true
print(tostring(operationList:alter("test3"))) --通过修改值方法向列表添加新值,输出 -1
print(tostring(operationList:alter("test3", nil, nil, true))) --通过修改值方法向列表添加新值,输出 0
print(tostring(operationList:alter("1"))) --获取 ID = 1 的值,因为存在,输出当前 ID 的值
print(tostring(operationList:alter("11"))) --获取 ID = 11 的值,因为不存在,输出 -1
print(tostring(operationList:alter("test1", 99))) --修改 ID = test1 的值为 99,因为存在,输出修改后的值
print(tostring(operationList:alter("11", 99))) --修改 ID = 11 的值为 99,因为不存在,输出 -1
print(tostring(operationList:alter("test1", 999, 100))) --修改 ID = test1 且当前值 = 100 的值为 999,因为ID存在 但 值判断值不相同,输出当前值
print(tostring(operationList:alter("test1", 999, 99))) --修改 ID = test1 且当前值 = 99 的值为 999,因为ID存在 且 值判断值相同,输出修改后的值
print(tostring(operationList:alter("11", 666, 999))) --修改 ID = 11 且当前值 = 999 的值为 666,因为ID不存在,输出 -1
print(tostring(operationList:compute("1"))) --计算 ID = 1 的值,其他参数为空,获得原值 + 1
print(tostring(operationList:compute("1", -2))) --计算 ID = 1 的值,计算值 = -2,获得原值 - 2
print(tostring(operationList:compute("1", -2, 499))) --计算 ID = 1 的值,计算值 = -2,且当前值 = 499 获得原值 - 2
print(tostring(operationList:compute("1", nil, 497))) --计算 ID = 1 的值,计算值 = nil,且当前值 = 497 获得原值
print(tostring(operationList:compute("1", -2, 499))) --计算 ID = 1 的值,计算值 = -2,但当前值 ~= 499 获得原值
print(tostring(operationList:compute("test6", -2, 499))) --计算 ID = test6 的值,计算值 = -2,但当前值 ~= 499 且 oCreate = nil ,不创建新子列,获得返回值 -1
print(tostring(operationList:compute("test6", -2, 499, true))) --计算 ID = test6 的值,计算值 = -2,但当前值 ~= 499 且 oCreate = true 创建新子列,获得返回值 0 --输出列表
print("\n")
for thisoL in listIterator(operationList) do
print("this ID is "..thisoL.id.."\nthis Num is "..thisoL.num.."\n")
end

2. 测试结果图:

以上就是全部内容了

Lua中对自定义二维表进行添加、修改、计算、删除、判断是否存在操作的更多相关文章

  1. linux 进阶2--C++读取lua文件中的变量、一维表、二维表

    lua 语言非常灵活,一般把lua 作为脚本文件,会用C++与之进行交互.最重要的是C++代码能读取到脚本中的变量.一维表.二维表. 这样有些参数就可以在lua文件进行更改,而不用重新更改C++代码. ...

  2. 议:如何将树形菜单形式的数据转化成HTML的二维表(相同内容需合并单元格)

    一般做OA类管理系统,经常涉及到“组织架构”的概念,那么像这种有上下层级关系的数据一般会做成树形菜单的方式显示,底层代码必定会用到递归算法.这篇随笔的目的就是要谈谈除了用树形菜单来显示这种上下层级关系 ...

  3. Qt信号槽机制的实现(面试的感悟,猜测每一个类保存的一个信号和槽的二维表,实际使用函数指针 元对象 还有类型安全的检查设定等等)

    因为面试时问了我这道题,导致我想去了解信号槽到底是如何实现的,于是贴着顺序看了下源码,大致了解了整个框架.网上关于信号槽的文章也很多,但是大部分都是将如何应用的,这里我就写一下我所理解的如何实现吧, ...

  4. java_web学习(四) 二维表的制作(初步接触MVC)

    我们需要做一个jsp页面,动态显示信息表的内容. 一.需求分析 1.  做一个实体类:StudentInfo (包含4个字段) 2.  如图模拟生成3条数据,本质上就是new StudentInfo ...

  5. Android生成自定义二维码

    前面说过两种二维码扫描方式,现在说如何生成自定义酷炫二维码.二维码生成需要使用Google开源库Zxing,Zxing的项目地址:https://github.com/ZBar/ZBar,我们只需要里 ...

  6. Excel:一维表和二维表 互转

    一.一维表转二维表 数据源: 一份流水账式的值班表,为了便于打印张贴,现在需要使其变成这样的样式: 也就是从一维表变成传说中的二维表. 1.新建查询 依次单击[数据]→[新建查询] →[从文件]→[从 ...

  7. C 二维数组,以及自定义二维数组

    C 二维数组,以及自定义二维数组 我们通常情况下是这样定义一个二维数组的: int a[10][15]; 我们分别查看一下a,a[0],*a 都是一样的值吧 我们可以这么理解: a是一个数组的数组 a ...

  8. Oracle【二维表管理:约束】

    1.简单的表创建和字段类型最简单的方式去创建表(没有添加主键之类的约束条件)[Oracle的字段类型]number:数值类型--整数类型:number(a) 总长度a--小数类型:number(a,b ...

  9. Oracle【二维表的维护】

    二维表的维护 --添加新的字段:alter table 表名 add 字段名 类型 [一般不加约束条件] ) 原表:新增字段后的表:修改原有的字段:[修改字段类型.修改字段名.删除字段] --修改字段 ...

  10. 数据可视化之PowerQuery篇(四)二维表转一维表,看这篇文章就够了

    https://zhuanlan.zhihu.com/p/69187094 数据分析的源数据应该是规范的,而规范的其中一个标准就是数据源应该是一维表,它会让之后的数据分析工作变得简单高效. 在之前的文 ...

随机推荐

  1. vue 3.0 总线程bus引入(mitt)

    vue 3.0 移除了 $on,$off 和 $once 方法,$emit 仍然是现有 API 的一部分,因为它用于触发由父组件以声明方式附加的事件处理程序. 官方推荐使用第三方类库.  mitt举例 ...

  2. 网络爬虫Python(一)

    1.爬取页面,打印页面信息 1 import requests 2 3 # get请求 4 response_get=requests.get("https://www.baidu.com& ...

  3. Elasticsearch使用示例

    简单示例 import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.extension.service.impl.Ser ...

  4. Python条件语句和基本数据类型

    1.if基本语句 if 条件: 内部代码块 else: ...... 2.if支持嵌套 if 1 == 1: if 2 == 2: print('走一步') else: print('走两步') el ...

  5. 题解 【POJ3728】The merchant(LCA)

    题意:一棵树有N个城市,每个城市商品价格不一样,Q个询问,问从u出发到达v点,每个城市只能经过一次的最大利润 max min数组存u城到u的第2^i个祖先路径上的最值 答案就是u-v路径上的最大值-最 ...

  6. HCIP-ICT实战进阶08-以太网链路的聚合和集群

    HCIP-ICT实战进阶08-以太网链路的聚合和集群 1 网络可靠性需求 网络可靠性可以从设备.链路多个层面实现, 保持当前设备或链路出现单点或者多点故障时保证网络服务不间断的能力. 网络可靠性 设备 ...

  7. win10上打包的qt程序放到win7上打不开的解决方法

    在win 10 上编写并打包发布了qt5.12.3 msvc2017 32bit的qt程序,在其他win10 电脑上都能正常运行,但是放到win7电脑上,运行exe时依次出现缺少"msvcp ...

  8. Gitbook部署

    title: Gitbook部署 # 标题 date: 2020-06-14 08:00:00 借助Gitbook,写自己的第一本电子书 Gitbook部署 一.电脑环境 Git 环境,我的电脑上已经 ...

  9. Centos 7 环境 安装todesk异常

    按照todesk官网安装步骤安装. 其实就两步就完成了,在自己虚拟机centos7环境下测试一切正常,但正式环境centos7.9环境下能安装,但安装完打不开,感觉是内核版本的问题. Todesk-- ...

  10. vscode 远程连接 linux 远程开发

    1. 安装拓展 remote-ssh 2. 设置云主机 git 控制台使用命令 ssh-keygen # 生成一个 ssh key # 登录远程主机 # ssh ubuntu@123.123.123. ...