lua-users home
lua-l archive

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


Mark Meijer wrote:
Sorry, I didn't see the last two replies (by Hans and Alex resp.)
until I posted my previous one.

never mind -)

in the original post there was a for i=1,n loop

when using (i)pairs one should be real carefull with such 'created holes'; the same is true for 0/1 based indexed tables

(lucky us that in most realistic cases things are quite intuitive)


t = { }

t[1], t[2], t[3] = 1, 2, 3 print("size",#t)

for k,v in ipairs(t) do print("a ipairs",k,v) end
for k,v in pairs (t) do print("a pairs ",k,v) end

t[0], t[8] = "not used", "not used" print("size",#t)

for k,v in ipairs(t) do print("a ipairs",k,v) end
for k,v in pairs (t) do print("a pairs ",k,v) end

t[2] = nil print("size",#t)

for k,v in ipairs(t) do print("a ipairs",k,v) end
for k,v in pairs (t) do print("a pairs ",k,v) end

t = { } print("")

t[0], t[8] = "not used", "not used" print("size",#t)

for k,v in ipairs(t) do print("b ipairs",k,v) end
for k,v in pairs (t) do print("b pairs ",k,v) end

t[1], t[2], t[3] = 1, 2, 3 print("size",#t)

for k,v in ipairs(t) do print("b ipairs",k,v) end
for k,v in pairs (t) do print("b pairs ",k,v) end

t[2] = nil print("size",#t)

for k,v in ipairs(t) do print("b ipairs",k,v) end
for k,v in pairs (t) do print("b pairs ",k,v) end


-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
     tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                             | www.pragma-pod.nl
-----------------------------------------------------------------