一,基礎
創(chuàng)新互聯(lián)公司專注于企業(yè)全網營銷推廣、網站重做改版、芷江網站定制設計、自適應品牌網站建設、H5響應式網站、成都商城網站開發(fā)、集團公司官網建設、成都外貿網站制作、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為芷江等各大城市提供網站開發(fā)制作服務。
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
結果:
當改變表的一個值以后,它的引用的值也發(fā)生了變化;
對于非表的一般常數(shù)來說,它的賦值不存在引用的問題;
2,table存儲
1)table里保存數(shù)據(jù),數(shù)據(jù)可以是任何類型,包括function。
2)table里也可以保存table
3)key代表數(shù)據(jù)存儲的位置
4)value就是用特定的key存儲的數(shù)據(jù)
二,記錄遇見的一個關于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的參數(shù)都是引用操作,在function中對table node的key,value的相關操作都是對原table t的操作;
2,node = node["TOKEN"] 這一步操作相當于把node的內存地址指向了node["TOKEN"]的內存地址(即t["GET"]["TOKEN"]的內存地址),故之后對node的操作,都會影響到t["GET"]["TOKEN"]。