Note though that it only counts the elements from index 1 up until the first nil.
Multidimentional arrays/Tables
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Multidimentional arrays/Tables
It's even worse than that. The length of a table with holes in its sequence is actually undefined. LuaJIT is kind of nice and stops counting at the first nil. Vanilla Lua does not.
Code: Select all
$ lua -e "print(#({1, 2, 3, nil, 4}))"
5
$ luajit -e "print(#({1, 2, 3, nil, 4}))"
3
Re: Multidimentional arrays/Tables
It depends, even in LuaJIT. Undefined is the right word.
Code: Select all
$ luajit -e 'print(#{1,2,nil,4})'
4
Re: Multidimentional arrays/Tables
Not on my system.
Code: Select all
local test = {1, 2, 3, 4, 5}
print(#test) -- 5
test[3] = nil
print(#test) -- 5
print()
for i = 1, #test do
print(i, test[i])
end
Code: Select all
5
5
1 1
2 2
3 nil
4 4
5 5
PS - I don't think this has been said yet, but if you know you're going to remove entries from a table (using table.remove rather than assigning nil) and you want to iterate through the whole table, it's generally a good idea to step through it backwards so your indexing doesn't get messed up.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Re: Multidimentional arrays/Tables
I got the double array table working, so yeah, I got the answer for that. For some reason my whole system was really wonky and too much work. I just wanted to do simple animation (that really wasnt really even needed) and with my idea how to do it was really complicated and too much work. I mean I could do it more easily if I start all over and rethink how I want to do it. But then again, it wasnt really needed feature at that point, so I took the whole idea out of the game. For now anyway.
I need more time to figure out how to think and do some tasks. Also I have other things going on, so I put Love on hold. I just wanted to come and see if I got any replys and to thank once again. I feel the community is really gold here. And I want to come back as soon as I got the time and figure out the things. And I also want to say that I enjoy to read and learn things that are a bit off topic too. Its like I learning always one or two new things. So I really dont mind and its so awesome that people are willing to help. Even with such basic things.
I need more time to figure out how to think and do some tasks. Also I have other things going on, so I put Love on hold. I just wanted to come and see if I got any replys and to thank once again. I feel the community is really gold here. And I want to come back as soon as I got the time and figure out the things. And I also want to say that I enjoy to read and learn things that are a bit off topic too. Its like I learning always one or two new things. So I really dont mind and its so awesome that people are willing to help. Even with such basic things.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 6 guests