lua实现深度拷贝table表
lua当变量作为函数的参数进行传递时,类似的也是boolean,string,number类型的变量进行值传递。而table,function,userdata类型的变量进行引用传递。故而当table进行赋值操作之时,table A
赋值给table B,对表B中元素进行操作自然也会对A产生影响,当然对B表本身进行处理例如B =nil或者将表B指向另一个表,则对A是没什么影响的;下面即是对lua table的深度拷贝。
deepcopy = function(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end return _copy(object)
end local testA = {,,,,} local testB = testA
testB[] = "我擦" local testC = deepcopy(testA)
testC[] = "我勒个去" for k , v in ipairs (testA) do
print("testA",k, v )
end print("============================") for k , v in ipairs (testC) do
print("testC",k, v )
end
运行结果如下:
testA 1 1
testA 2 我擦
testA 3 3
testA 4 4
testA 5 5
============================
testC 1 1
testC 2 我勒个去
testC 3 3
testC 4 4
testC 5 5
lua实现深度拷贝table表的更多相关文章
- lua的克隆函数,table的深度拷贝
--深度拷贝Table function DeepCopy(obj) local InTable = {}; local function Func(obj) if type(obj) ~= &quo ...
- Lua 学习之基础篇四<Lua table(表)>
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- [lua]紫猫lua教程-命令宝典-L1-01-07. table表
L1[table]01. table表的定义与赋值 小知识:声明表的例子 xx={}--创建一个空表xx --给这表的元素赋值 test="a" xx[test]="a& ...
- lua table表
lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...
- 关于 lua table表存储函数且运用
--table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意类型的值来做数组的索引,但这个值不能是nil--lua tabl ...
- lua基础【三】唯一数据结构table表
--[[ 数据结构table对象(一种动态分配的对象) lua中的表操作.table类型实现了"关联数组的". "关联数组是一种具有特殊索引方式的数组" 能够通 ...
- Lua table(表)
table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组.字典等. Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil. Lua ta ...
- lua 打印 table 拷贝table
-- 打印table function print_lua_table (lua_table, indent) if lua_table == nil or type(lua_table) ~= &q ...
- CREATE TABLE 表名 AS SELECT 语句
1.新表不存在复制表结构即数据到新表 ? 1 2 create table new_table select * from old_talbe; 这种方法会将old_table中所有的内容都拷贝过来, ...
随机推荐
- [转]MySQL批量更新死锁案例分析
文章出处:http://blog.csdn.net/aesop_wubo/article/details/8286215 问题描述 在做项目的过程中,由于写SQL太过随意,一不小心就抛了一个死锁异常, ...
- EditPlus 3.1
User:GNU Serial:918A8-20DD8-44ZA1-B0W4A-13T66
- [LeetCode] 452 Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 数据库软件dbForge Studio for MySQL更新至v.6.1
本文转自:慧都控件网 说到MariaDB,这个数据库算是MySQL的一个分支.现在非常的流行,很多地方都能看到它的身影.MariaDB作为一种新的数据库管理系统,在短时间内获得如此高的关注度.这也是D ...
- bdb mvcc: buffer 何时可以被 看到; mvcc trans何时被移除
# txn.h struct __db_txnregion SH_TAILQ_HEAD(__active) active_txn; SH_TAILQ_HEAD(__mvcc) mvcc_txn; # ...
- Struts2基础
访问ServletApi (1)用ActionConten访问Api, ActionContent.getContext().getSession().put("user",&qu ...
- hdu 5104 素数打表水题
http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...
- python 装饰器初步学习
第一步 简单函数 ''' 简单的函数:调用两次''' def myfunc(): print ('myfunc() called.') myfunc() myfunc() 第二步 装饰器为调用函数提供 ...
- 解决eclipse-helios中Errors running builder JavaScript Validator的问题(转)
原文地址:http://hi.baidu.com/%B3%BF%D1%F4%C2%FE%B2%BD/blog/item/2528f6de3ca90955ccbf1a3f.html 最近下载了eclip ...
- testMarkDown
title: git常用命令 date: 2015-10-27 10:28:25 categories: git tags: git 添加 SSH 公钥 Windows 平台中,可以使用 Git Ba ...