Originally Posted by d87
Code:
local t3 = {nil, nil, 4, 7, nil, 4}
t3[5]= 2 -- #t3 = 6
t3[7]= 2 -- #t3 = 0
Also, if you try to expand your array, sizearray is recalculated to first boundary it founds.
|
I've tested this with the following function,
sizearray doesn't ever seem to decrease like said.
Lua Code:
function tAppend(tbl,...)
-- This should allow unpack() to work with embedded nils as if {...} was used
-- We need to keep the array allocation intact, Lua ignores setting the next index to nil
-- and creates gaps, pouring the next values into the non-array section of the table
local tlen,previsnil=#tbl,false;
for i=1,select("#",...) do
local id,val=tlen+i,select(i,...);
tbl[id]=val or false;-- Force to an actual value
if previsnil then tbl[id-1]=nil; end
previsnil=(val==nil);
end
if previsnil then tbl[#tbl]=nil; end
return tbl;
end
Running the following code allows it to resize the table a few times to give it a chance to recalculate the
sizearray value, but the embedded nils are still counting towards the size.
Code:
local t=tAppend({},1,nil,nil,4,nil,6,7,8);
print(#t); -- Prints 8
To make the process better understood, this is the same as the following:
Code:
local t={};
t[1]=1;
t[2]=false;
t[3]=false; t[2]=nil;
t[4]=4; t[3]=nil;
t[5]=false;
t[6]=6; t[5]=nil;
t[7]=7;
t[8]=8;
print(#t); -- Prints 8