lua table引用问题

发布时间:2020-07-06 12:02:26 作者:leejia1989
来源:网络 阅读:5324


一,基础

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")
local t = {["GET"] = {["/a"] = "f"}}

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

   node = node["TOKEN"]
   ngx.log(ngx.ERR, "1", cjson.encode(t["GET"]))
   ngx.log(ngx.ERR, "1", cjson.encode(node))
   ngx.log(ngx.ERR, "1", tostring(node))

   node["TOKEN"] = "123"
   ngx.log(ngx.ERR, "2", cjson.encode(t["GET"]))
   ngx.log(ngx.ERR, "2", cjson.encode(node))
   ngx.log(ngx.ERR, "2", tostring(node))
end

hehe(t["GET"])
ngx.say("ok")

nginx日志中的结果:

2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):8: hehe(): 0{"\/a":"f","TOKEN":{}}, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):9: hehe(): 0{"\/a":"f","TOKEN":{}}, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):10: hehe(): 0table: 0x41dfca60, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):13: hehe(): 1{"\/a":"f","TOKEN":{}}, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):14: hehe(): 1{}, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):15: hehe(): 1table: 0x41e011e0, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):18: hehe(): 2{"\/a":"f","TOKEN":{"TOKEN":"123"}}, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):19: hehe(): 2{"TOKEN":"123"}, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"
2017/07/10 15:28:16 [error] 20400#0: *749 [lua] access_by_lua(nginx.conf:138):20: hehe(): 2table: 0x41e011e0, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8888"


结果分析:

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

2,node = node["TOKEN"] 这一步操作相当于把node的内存地址指向了node["TOKEN"]的内存地址(即t["GET"]["TOKEN"]的内存地址),故之后对node的操作,都会影响到t["GET"]["TOKEN"]。



推荐阅读:
  1. Lua 面向对象
  2. Lua之table(表)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

引用 lua tables

上一篇:Bash定制化之ACCSI字符图

下一篇:Grafana 安装配置 对接zabbix

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》