Answer the question
In order to leave comments, you need to log in
"Slice" nested table at a certain level of the "tower" of tables multiple nested in depth (into themselves)?
Tell me why when building a "tower" from tables (without explicit keys) in depth, you can "cut" a table at a certain level like this: (do not forget about the theory - in lua variables contain references to tables, not copies of tables!)
local t = {{{{{}}}}} -- 4 вложенных
local g = t
local tst = t[1][1][1]
print(t[1][1][1][1]) -- table:
t[1][1] = t[1][1][1]
print(t[1][1] == tst, t[1][1][1][1] ) -- true, nil срезалась и "хвост" подтянула!
-- А так нельзя
print( t[1][1][1][1] ) -- table:
local a = t[1][1]
local b = t[1][1][1]
a = b
print( t[1][1][1][1] ) -- table: хвост не подтянут!
print( t[1][1] == g[1][1], t[1][1]== tst) -- true, false
-- и срез не произошёл! Проверим ещ переменные:
print( a == g[1][1], b == g[1][1], a==b) -- false, false, true
print(a == g[1][1][1], b == g[1][1[1]]) -- true, true
print(a == t[1][1][1], b == t[1][1[1]]) -- true, true
print(t==g) -- true
local pred = t
while (pred) do
pred = pred[1]
if pred == t[1][1] then
pred = pred[1] -- не работает так. см 2 пример
end
end
Answer the question
In order to leave comments, you need to log in
You can not do it this way
local a = t[1][1]
local b = t[1][1][1]
a = b
local a = t[1]
local b = t[1][1][1]
a[1] = b
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question