Multiple distinct objects: Difference between revisions

Line 596:
 
=={{header|Lua}}==
<lang Lua>-- This concept is relevant to tables in Lua
<lang Lua>local table1 = {1,2,3}</lang>
 
-- The following will create a table of references to table1
local copyTabrefTab = {}
<lang Lua>function nRefs (t, n)
for i = 1, local10 do refTab[i] = {}table1 end
for i = 1, 10 do refTab[n] = t end
return refTab
end
 
-- Instead, tables should be copied using a function like this
local mistake = nRefs(table1, 10)</lang>
<lang Lua>function nRefscopy (t, n)
Instead, tables should be copied using a function like this
<lang Lua>function copy (t)
local new = {}
for k, v in pairs(t) do new[k] = v end
return new
end</lang>
Now we can create a table of independent copies of table1
<lang Lua>function nCopies (t, n)
local copyTab = {}
for i = 1, 10 do copyTab[i] = copy(table1) end
return copyTab
end
 
-- Now we can create a table of independent copies of table1
local tableOfTables = nCopies(table1, 10)</lang>
local copyTab = {}
for i = 1, 10 do refTabcopyTab[ni] = tcopy(table1) end</lang>
 
=={{header|Mathematica}}==
Anonymous user