lua table引用问题

一,基础

1,在lua中,table只是传递引用,所以不能用简单的 "=" 来copy两个表,并试图修改一个表中的值。

tb = {}
tb.a = 11
tb.b = 22
tb_ref = tb
function p(tip)
  print("--------------------------"  .. tip)
  print("tb.a = " .. tb.a .. "    " .. "tb.b = " .. tb.b)
  print("tb_ref.a = " .. tb_ref.a .. "    " .. "tb_ref.b" .. tb_ref.b)
end
p("原始")
tb_ref.a = 33
p("修改了引用的a = 33,原来的a也变了")
tb.b = 44
p("修改了原始的b = 44,引用的b也变了")
print("----------------------非表test")
a = 1
c = a
c = 3
print("a = " .. a)
print("c = " .. c) 
打印结果:
--------------------------原始
tb.a = 11    tb.b = 22
tb_ref.a = 11    tb_ref.b22
--------------------------修改了引用的a = 33,原来的a也变了
tb.a = 33    tb.b = 22
tb_ref.a = 33    tb_ref.b22
--------------------------修改了原始的b = 44,引用的b也变了
tb.a = 33    tb.b = 44
tb_ref.a = 33    tb_ref.b44
----------------------非表test
a = 1
c = 3

结果:

当改变表的一个值以后,它的引用的值也发生了变化;

对于非表的一般常数来说,它的赋值不存在引用的问题;

2,table存储

1)table里保存数据,数据可以是任何类型,包括function。

2)table里也可以保存table

3)key代表数据存储的位置

4)value就是用特定的key存储的数据

二,记录遇见的一个关于table的问题

代码如下:

local cjson = require("cjson.safe")

function hehe(node)
    node["TOKEN"] = node["TOKEN"] or {}
    ngx.log(ngx.ERR, cjson.encode(node), "0", tostring(node))
                  
    node = {}
    ngx.log(ngx.ERR, cjson.encode(node), "1", tostring(node))
end

local t = {["GET"] = {["/a"] = "f"}}
hehe(t)
ngx.log(ngx.ERR, cjson.encode(t), "2", tostring(t))
ngx.say("ok")

nginx日志中的结果:

2017/07/07 16:29:34 [error] 6183#0: *695 [lua] access_by_lua(nginx.conf:128):6: hehe(): {"TOKEN":{},"GET":{"\/a":"f"}}0table: 0x41383288, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/07 16:29:34 [error] 6183#0: *695 [lua] access_by_lua(nginx.conf:128):8: hehe(): {}1table: 0x4138ace0, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/07 16:29:34 [error] 6183#0: *695 [lua] access_by_lua(nginx.conf:128):13: {"TOKEN":{},"GET":{"\/a":"f"}}2table: 0x41383288, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"

结果分析:

lua中table相关操作包括做为function的参数都是引用操作,在function中对table node的key,value的相关操作都是对原table t的操作;

node = {}操作也是引用操作,node变量的内存地址指向空table {}的内存地址,则table node的内存地址就和table t的不一样了,这个操作并不会影响table t;

时间: 2024-08-26 20:15:51

lua table引用问题的相关文章

Lua弱引用table

弱引用table 与python等脚本语言类似地,Lua也采用了自动内存管理(Garbage Collection),一个程序只需创建对象,而无需删除对象.通过使用垃圾收集机制,Lua会自动删除过期对象.垃圾回收机制可以将程序员从C语言中常出现的内存泄漏.引用无效指针等底层bug中解放出来. 我们知道Python的垃圾回收机制使用了引用计数算法,当指向一个对象的所有名字都失效(超出生存期或程序员显式del了)了,会将该对象占用的内存回收.但对于循环引用是一个特例,垃圾收集器通常无法识别,这样会导

树形打印lua table表

local print = print local tconcat = table.concat local tinsert = table.insert local srep = string.rep local type = type local pairs = pairs local tostring = tostring local next = next function print_lua_table (lua_table, indent) if not lua_table or t

从实现求差集介绍lua table需要注意的一些问题

用lua实现的求两个table的差集(只支持一维table) 1.lua table 判空: table 判空,用的了next()函数. next()函数说明:运行程序来遍历表中的所有域. 第一个参数是要遍历的表,第二个参数是表中的某个键. next 返回该键的下一个键及其关联的值. 如果用 nil 作为 第二个参数调用 next 将返回初始键及其关联值. 当以最后一个键去调用,或是以 nil 调用一张空表时, next 返回 nil. 如果不提供第二个参数,将认为它就是 nil. 可以用 ne

lua table integer index 特性

table.maxn (table) Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.) 返回表中最大的正数值index. 说明: 1. 此接口不是统计表中元素的

Lua table pair和ipair区别

官方描述: ipairs (t) Returns three values: an iterator function, the table t, and 0, so that the construction for i,v in ipairs(t) do body end will iterate over the pairs (1,t[1]), (2,t[2]), ···, up to the first integer key absent from the table. pairs (

Lua table 的长度问题

直入主题 我们要取lua table的长度时习惯这样 local tb = {1,2,3,4} print(#tb) 这时候,输出理所当然的是4,如果在程序中我们需要判断长度时使用这样的方法,再看一下 tb[1] = nil print(#tb) 输出还是4,似乎有点背离我们的意愿 我们想知道这个table里面是不是有4个值,但是有一个值被置空了,输出结果却没有及时告诉我们 发生这样问题的原因是,lua在初始化table的时候,会给这个table分配值存储的空间,代码里面tb初始化包含4个数字值

(转) 关于lua table是否为空的判断

在项目的脚本lua中经常有这样的需求, 1.local a = {} 2.对a进行处理 3.对a是否为空表进行判断 关于对a是否为空表的判断,我发现有些代码如此做: if a == {} then 这样的结果就是a == {}永远返回false,是一个逻辑错误.因为这里比较的是table a和一个匿名table的内存地址. 也有些代码如此做: if table.maxn(a) == 0 then 这样做也不保险,除非table的key都是数字,而没有hash部分. 难道真的要遍历table发现有

Lua table转C++数组

为了方便,这里数组用vector表示. C++层代码如下: static int LuaTable2Vector(lua_State *luaState) { int count = luaL_getn(luaState, 1); std::vector<std::string> vecNameList; for (int i = 0; i < count; i++) { lua_rawgeti(luaState, 1, i+1); const char *strName = lua_t

Lua Table pairs输出顺序问题 (版本差异 解决数字索引间断并兼容字符串索引)

问题标签: Lua Table 迭代器;Lua Table 输出顺序; Lua Table 顺序输出;Lua Table 数字索引 字符串索引;Lua Table pairs; 问题背景: 使用pairs输出table时,其输出顺序与通常认知不相符. 例如使用pairs输出如下table T = { [1] = "1", [2] = "1", [3] = "1", [4] = "1", [5] = "1",