像這樣的問題,你可能應該重新考慮你的設計選擇。您所描述的n表中的表格類似於一個樹形圖,其中字段box
包含對子節點的引用。
當您的註釋狀態時,您需要修改位於該樹中動態路徑上的節點。一個簡單的功能是顯而易見的:
local function follow_path(start_node, path,i)
--takes start node and array of indices to follow
--returns table that is stored in the box-tree at this path
--i is for internal recursion
i=i or 1
if not path[i] then
return start_node
else
local new_start=start[path[i]]
assert(new_start,"No node is on this path")
return follow_path(new_start,path,i+1)
end
end
local function follow_path_and_rename(start_node,path,field_name,new_value)
local box = follow_path(start_node,path)
box[field_name] = new_value
end
但是,這取決於你爲什麼需要修改與動態路徑可能有更堅實的方法的一個節點。從來沒有傷害的一件事是它的創作過程中給你的每一個箱子的唯一標識符:
local new_box
do
local box_count=0
new_box = function(name)
box_count=box_count+1
local box = {
unique_id=box_count,
name=name,
box={}
}
return box
end
end
有了,你可以例如創建索引表,其中的箱子總是通過自己的ID訪問:
local index={}
local add_new_box = function(name)
local box = new_box(name)
index[ box.unique_id] = box
return box
end
或者如果這是不可接受的,您可以始終通過樹的節點搜索保證具有唯一值的節點。
但事情是:所有表確實已經有唯一的標識符。這是他們的地址,當您執行a = {}
時,該值將被分配到a
。唯一的區別是,和unique_id
是: 1)a
是不太可讀。 2)如果你知道a
,你不需要搜索它。
所以,看看你的問題,看看,問問自己:?? 「如果這需要來自我爲什麼不能得到unique_id
替代指標序列爲什麼我不能得到box
本身,而不是unique_id
? 「
'local t,n = _G,4;對於j = 1,n do t = t.Box [1] end; t.Name =「更改」 –
@EgorSkriptunoff,這應該是足夠的答案;我測量;) –
@PaulKulchenko - 但它很適合單行評論! :-) –