2015-08-18 12 views
1

我想找到的條目數爲test[0]的Lua複雜的表格/列表大小

test = {} 
test[0] = {} 
test[0].x = {} 
test[0].x[0] = 1 
test[0].x[1] = 1 
test[0].x[2] = 1 
test[0].y = {} 
test[0].y[0] = 1 

我期待table.getn(test[0])爲2條目test[0].xtest[0].y,但它的結果爲0這是爲什麼,和我需要做些什麼來獲得我正在尋找的東西?

+0

我發現'for _對(tab)做count = count + 1 end'這個問題。 – Lono

+1

我不知道你需要這個計數... – lhf

回答

2

注意table.getn在Lua 5.0一直以來的Lua 5.1

一個表的大小是僅適用於一個表(即序列部分,具有正數字鍵從1一些替換爲#操作者號碼n,和n是大小)。

在這個例子中,test[0]只有兩個kesy "x""y"。因此其尺寸爲0

1

table.getn和lua 5.1 length operator被定義爲對「列表」或陣列進行操作。你的桌子不是一個。它沒有數字指標。

所以結果未定義在lua 5.1中(儘管它將爲零),並且在lua 5.0中爲0,因爲大小被定義爲one less the first integer index with a nil value,這是整數索引1

另外值得注意的是,table.getn(test[0].x)將返回2table.getn(test[0].y)將返回0(因爲LUA陣列在1啓動)。