当前位置:网站首页>Lua对table进行深拷贝
Lua对table进行深拷贝
2022-07-28 11:16:00 【漫步云巅】
有时候,在开发当中,想对一个table进行相关的操作,但是操作完之后能够不对原本的table产生修改,这时候就需要对table进行一个拷贝,然后使用这个拷贝的table进行相关的操作。
直接对table进行等号赋值,那是不行的,这样拿到的还是原本table的引用,修改时会把原本table的值一起修改:

所以这时候就需要对table进行深拷贝
function DeepCopy(tmp)
if nil == tmp then return nil end
local res = {}
for key, val in pairs(tmp) do
if type(val) == "table" then
res[key] = DeepCopy(val)
else
res[key] = v
end
end
return res
end
local a = { }
a.num = 1
local b = DeepCopy(a)
b.num = 3
print(a.num)这样得到的结果就是1了,拷贝后修改的值不影响原本的table。
边栏推荐
- Multithreading and high concurrency (III) -- source code analysis AQS principle
- Why does acid food hurt teeth + early periodontitis
- R language - some metrics for unbalanced data sets
- 哪位大神帮看下 oracle number类型解析 怎么搞 Struct{scale=15,val
- A new mode of one-stop fixed asset management
- Quickly deploy mqtt clusters on AWS using terraform
- 【补题日记】[2022牛客暑期多校2]H-Take the Elevator
- How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
- Excel shortcut keys (letters + numbers) Encyclopedia
- 直接插入排序与希尔排序
猜你喜欢

What is the process of switching c read / write files from user mode to kernel mode?

Direct insert sort and Hill sort

Today's sleep quality record 74 points

数字孪生轨道交通:“智慧化”监控疏通城市运行痛点

Blackboard cleaning effect shows H5 source code + very romantic / BGM attached

A new mode of one-stop fixed asset management

Simple selection sort and heap sort
![[general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials](/img/46/830b7703ae7cbfa6051137061560c2.png)
[general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials

Four advantages of verification code to ensure mailbox security

Understand how to prevent tampering and hijacking of device fingerprints
随机推荐
A new mode of one-stop fixed asset management
15、用户web层服务(三)
Why does acid food hurt teeth + early periodontitis
AlexNet—论文分析及复现
LabVIEW AI视觉工具包(非NI Vision)下载与安装教程
Go deadlock - when the channel meets mutex
Deployment and use of Minio distributed object storage
jar 包内文件的遍历以及文件的拷贝
Cvpr2021 pedestrian re identification /person re identification paper + summary of open source code
DNS series (III): how to avoid DNS spoofing
Introduction to the usage of SAP ui5 image display control avatar trial version
Using C language to realize bidirectional linked list
P5472 [NOI2019] 斗主地(期望、数学)
A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
Techniques for visualizing large time series.
大佬们,问下,这个不能checkpoint,因为有个jdbc的task任务状态是FINISHED,
Anonymous implementation class object of interface
R language uses LM function to build regression model and regression model for transformed data (for example, it is necessary to build regression model for X and y, but they have no linear relationshi
Redis installation
【补题日记】[2022杭电暑期多校2]K-DOS Card