Multidimentional arrays/Tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Xii
Citizen
Posts: 90
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Multidimentional arrays/Tables

Post 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.
grump
Party member
Posts: 721
Joined: Sat Jul 22, 2017 7:43 pm

Re: Multidimentional arrays/Tables

Post 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
User avatar
pgimeno
Party member
Posts: 2611
Joined: Sun Oct 18, 2015 2:58 pm

Re: Multidimentional arrays/Tables

Post by pgimeno »

It depends, even in LuaJIT. Undefined is the right word.

Code: Select all

$ luajit -e 'print(#{1,2,nil,4})'
4
milon
Party member
Posts: 123
Joined: Thu Jan 18, 2018 9:14 pm

Re: Multidimentional arrays/Tables

Post 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:

Code: Select all

5
5

1	1
2	2
3	nil
4	4
5	5
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.
Bowlman
Prole
Posts: 9
Joined: Sun Apr 04, 2021 7:51 pm

Re: Multidimentional arrays/Tables

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 14 guests