--tables for the 1st example
local table1EX1={1,2,3}
local table2EX1={4,5}
local temp={}
-- 1st example:
temp=table1EX1
for i=1,2 do
temp[3+i]=table2EX1[i]
print("#temp = "..#temp)
end
-- output from the print command:
-- #temp = 4
-- #temp = 5
-- this is what I intended to do but I need it to adapt to a changing table size
--tables for the 2nd example
local table1EX2={1,2,3}
local table2EX2={4,5}
local temp={}
-- 2nd example:
temp=table1EX2
for i=1,#table2EX2 do
temp[#table1EX2+i]=table2EX2[i]
print("#temp = "..#temp)
end
-- output from the print command:
-- #temp = 4
-- #temp = 6
-- this is what I tried and where I noticed something is wrong
#table_name only ever returns a single value and that's the numeric range from the first to the last entry within the named table.
Tests to confirm this: #1:
local tab={-1,0,_,nil,"a",nil,"x",_,3}
print(#tab) -- returns 9
#2:
local tab={1,2,3}
print(#tab) -- returns 3
tab[2]=nil
print(#tab) -- returns 3
#3:
local tab={1,2,3}
print(#tab) -- returns 3
tab[3]=nil
print(#tab) -- returns 2
I don't understand what's going wrong and why between example#1 and example#2: The code should work the same: table1EX1 and table1EX2 never receive any changes to their contents or size, because the temp variable is used to store the values in both cases. Yet in example#2 the code suddenly causes a change to the contents of table1, as can be seen by the print command output.
While the entire issue can be avoided by assigned variables, like...
--tables for the 2nd example
local table1EX2={1,2,3}
local table2EX2={4,5}
local temp={}
-- 2nd example:
temp=table1EX2
local x,y=#table2EX2,#table1EX2
for i=1,x do
temp[y+i]=table2EX2[i]
print("#temp = "..#temp)
end
...I'd like to understand what's wrong and why. Also what's the '#' properly called in English in this case ? Sharp, like in C# ? Lozenge, like the dictionary shows ?