View Single Post
05-08-12, 12:13 AM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Originally Posted by d87 View Post
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:
  1. function tAppend(tbl,...)
  2. --  This should allow unpack() to work with embedded nils as if {...} was used
  3. --  We need to keep the array allocation intact, Lua ignores setting the next index to nil
  4. --  and creates gaps, pouring the next values into the non-array section of the table
  5.     local tlen,previsnil=#tbl,false;
  6.     for i=1,select("#",...) do
  7.         local id,val=tlen+i,select(i,...);
  8.         tbl[id]=val or false;-- Force to an actual value
  9.         if previsnil then tbl[id-1]=nil; end
  10.         previsnil=(val==nil);
  11.     end
  12.     if previsnil then tbl[#tbl]=nil; end
  13.     return tbl;
  14. 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
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote