Page 2 of 2
Re: Multidimentional arrays/Tables
Posted: Mon Apr 12, 2021 9:52 pm
by Xii
milon wrote: ↑Mon Apr 12, 2021 8:07 pm
You can also use the # prefix to get the size of a table
Note though that it only counts the elements from index 1 up until the first nil.
Re: Multidimentional arrays/Tables
Posted: Mon Apr 12, 2021 10:10 pm
by grump
Xii wrote: ↑Mon Apr 12, 2021 9:52 pm
milon wrote: ↑Mon Apr 12, 2021 8:07 pm
You can also use the # prefix to get the size of a table
Note though that it only counts the elements from index 1 up until the first nil.
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
Posted: Tue Apr 13, 2021 2:31 am
by pgimeno
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
Posted: Tue Apr 13, 2021 12:57 pm
by milon
Xii wrote: ↑Mon Apr 12, 2021 9:52 pm
milon wrote: ↑Mon Apr 12, 2021 8:07 pm
You can also use the # prefix to get the size of a table
Note though that it only counts the elements from index 1 up until the first nil.
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
The output of the above is:
EDIT - As fun as debating arrays is (lol), I think we may be straying from Bowlman's original question. Bowlman, are you satisfied with the answers you've received? I'd hate to hijack an honest question to quibble over details.
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.
Re: Multidimentional arrays/Tables
Posted: Fri Apr 16, 2021 10:25 am
by Bowlman
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.