lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2009/12/29 h visli <h_visli@yahoo.com.cn>:
> Yes, I also noticed those words, but it still confusing me, maybe my English is poor, can you put more detailed explanation? Thanks In Advance!

Reading manuals can be hard, especially since they only say the
important things once!

Look at this case
{10,nil,20,nil}
  1   2   3  4

So the values preceding the 'hole' are 10 (at 1) and 20 (at 3). So #
can report a length of either 1 or 3.

Moral of story: don't trust # for a table with holes.  Keep the actual
count and use that:

t = {n=4,10,nil,20,nil}
for i = 1,t.n do print(t[i]) end  -- will print out all values, even if nil

If you need to put nil in a table, think of using a 'placeholder'
value which shall represent nil

NIL = {}
t = {10,NIL,20,NIL}

and then convert any NIL values into actual nils.

steve d.


steve d.