I'm missing something here... transitioning from other languages to lua and I've got a gap somewhere I was hoping you could point it out to me and give me some reference material to fill in the gap...
I don't seem to be understanding tables correctly -- mostly the '#' meta and iterating over them. Here is some code:
Lua Code:
function testTableAdd(count)
local t = {}
for i = 1, count do
print("Adding "..i)
t[i] = "Yes please ".. i
print(#t)
end
print("We counted "..#t.." times")
return t
end
print(testTableAdd(5))
function testTableAddOutOfOrder()
local t = {}
t[3] = "three"
t[5] = "five"
t[15] = "wow"
print("We counted "..#t.." times")
return t
end
print(testTableAddOutOfOrder())
I was expecting "We counted 5 times" and "We counted 3 times", but the 2nd one says 0 -- but it has the 3 entries.
For the first I can iterate through them with a for i = 1, #t do but the second one that doesn't work since the # is 0. Using a 'pairs' do I got through the second. Hmmm... typing it out it is making more sense why the second loop was failing, because the indexes were different. But some background would still help.
I wonder in my bigger case if I should just fill in the gaps with nils and that might just solve my problem...
Is there a way to add to the table, just to the end of it?
I'm used to
t << 'new entry'
in something like ruby.
Thanks.