Lua table reference problem
First, the foundation
1. In lua, table simply passes references (that is, both variables point to the same piece of memory space), so you can't copy two tables with a simple "=" and try to change the values in one table.
Tb = {} tb.a = 11tb.b = 22tb_ref = tbfunction 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) endp ("original") tb_ref.a = 33p ("modified the referenced a = 33, the original an also changed") tb.b = 44p ("modified the original b = 44, the referenced b also changed") print ("--non-table test") a = 1c = ac = 3print ("a =".. A) print ("c =".. C) print result:-- original tb.a = 11 tb.b = 22tb_ref.a = 11 tb_ref.b22-- modified the referenced a = 33 The original a has also changed tb.a = 33 tb.b = 22tb_ref.a = 33 tb_ref.b22-- modified the original b = 44, and the quoted b has also changed tb.a = 33 tb.b = 44tb_ref.a = 33 tb_ref.b44-- non-table testa = 1c = 3
Results:
When you change a value of a table, the value of its reference also changes.
For a general constant that is not a table, its assignment does not have the problem of reference.
2Maketable storage
1) data is stored in table, which can be of any type, including function.
2) table can also be saved in table
3) key represents the location of data storage
4) value is the data stored in a specific key
Second, record a question about table
The code is as follows:
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)) endhehe (t ["GET"]) ngx.say ("ok")
The results in the nginx log:
2017-07-10 15:28:16 [error] 20400: * 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 access_by_lua 8888" 2017-07-10 15:28:16 [error] 20400 nginx.conf:138: * 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 access_by_lua 8888" 2017-07-10 15:28:16 [error] 20400 808: * 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 access_by_lua 8888" 2017-07-10 15:28:16 [error] 20400 nginx.conf:138: * 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 access_by_lua 8888" on 2017-07-10 15:28:16 [error] 20400 nginx.conf:138: * 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 access_by_lua 8888" 2017-07-10 15:28:16 [error] 20400 808: * 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 access_by_lua 8888" 2017-07-10 15:28:16 [error] 20400: * 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 access_by_lua 8888" on 2017-07-10 15:28:16 [error] 20400: * 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 GET 8888" 2017-07-10 15:28:16 [error] 20400 nginx.conf:138: * 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 GET / 8888"
Result analysis:
1the operations related to table in function, including the parameters as function are all reference operations, and the operations related to the key,value of table node in function are all operations on the original table t.
2 the operation of node node = node ["TOKEN"] is equivalent to pointing the memory address of node to the memory address of node ["TOKEN"] (that is, the memory address of t ["GET"] ["TOKEN"]), so the subsequent operation on node will affect t ["GET"] ["TOKEN"].