Page 1 of 3
How can one get a table's length?
Posted: September 29th, 2017, 1:27 pm
by kyriakos876
Hello, I was wondering why does this not work. I want to get the length of the "DeletedBoxes " table using this:
Code: Select all
DeletedBoxes = {2, 9, 14, 3, 8}
local NumberOflistElements = 0
local count = 0
while DeletedBoxes[count] ~= nil do
count = count + 1
end
NumberOflistElements = count
Is there any function already in lua that does it like other languages?
Re: How can one get a table's length?
Posted: September 29th, 2017, 1:36 pm
by jsmorley
kyriakos876 wrote:Hello, I was wondering why does this not work. I want to get the length of the "DeletedBoxes " table using this:
Code: Select all
DeletedBoxes = {2, 9, 14, 3, 8}
local NumberOflistElements = 0
local count = 0
while DeletedBoxes[count] ~= nil do
count = count + 1
end
NumberOflistElements = count
Is there any function already in lua that does it like other languages?
Yes.
Code: Select all
DeletedBoxes = {2, 9, 14, 3, 8}
NumberOflistElements = #DeletedBoxes
Re: How can one get a table's length?
Posted: September 29th, 2017, 1:38 pm
by kyriakos876
jsmorley wrote:Yes.
Code: Select all
DeletedBoxes = {2, 9, 14, 3, 8}
NumberOflistElements = #DeletedBoxes
Knowledge is power. Thanks again
Re: How can one get a table's length?
Posted: September 29th, 2017, 1:40 pm
by jsmorley
You can also use table.getn() to retrieve the size of a table. The "#" char is more or less a shortcut to that.
https://www.lua.org/pil/19.1.html
Re: How can one get a table's length?
Posted: September 29th, 2017, 1:42 pm
by jsmorley
Re: How can one get a table's length?
Posted: September 29th, 2017, 1:45 pm
by kyriakos876
jsmorley wrote:You can also use table.getn() to retrieve the size of a table. The "#" char is more or less a shortcut to that.
]
That seems unnecessary to exist when you can do it with just a hashtag... Though I'm new and I can't imagine of a reason so yea... Imma stay with the hashtag
Also, what kind of magic was that
http://lmgtfy.com/?q=getting+the+size+of+a+table+in+lua?
I love how ironic I can be with that...
Yea... I googled it but I got this "
https://stackoverflow.com/questions/2705793/how-to-get-number-of-entries-in-a-lua-table" and some other similar posts and I never figured out the actual answer... lol #feelsStupid
Re: How can one get a table's length?
Posted: September 29th, 2017, 1:57 pm
by kyriakos876
Okay, now an actual question.
How can I add a new thing after the table's last element?
Code: Select all
DeletedBoxes = {2, 3, 5, 6}
NumberOflistElements = #DeletedBoxes
DeletedBoxes[NumberOflistElements+1] = 9
I have this that adds the number 9 after the last element, but how can I save it in the lua?
(Then I suppose to do that with a number that came from an .ini file I just call the function with this number right?)
Re: How can one get a table's length?
Posted: September 29th, 2017, 2:04 pm
by jsmorley
kyriakos876 wrote:Okay, now an actual question.
How can I add a new thing after the table's last element?
Code: Select all
DeletedBoxes = {2, 3, 5, 6}
NumberOflistElements = #DeletedBoxes
DeletedBoxes[NumberOflistElements+1] = 9
I have this that adds the number 9 after the last element, but how can I save it in the lua?
(Then I suppose to do that with a number that came from an .ini file I just call the function with this number right?)
Code: Select all
DeletedBoxes = {2, 3, 5, 6}
NumberOflistElements = #DeletedBoxes
table.insert(DeletedBoxes, #DeletedBoxes, 9)
https://www.lua.org/pil/19.2.html
If you provide an "insertion point" number in the call to table.insert, it will insert the new value at that point, if not, it will append the new value as a new "row" at the bottom of the table.
So in my example the #DeletedBoxes parameter isn't needed, I just put it there for illustration. This would do the same thing:
Code: Select all
DeletedBoxes = {2, 3, 5, 6}
NumberOflistElements = #DeletedBoxes
table.insert(DeletedBoxes, 9)
Re: How can one get a table's length?
Posted: September 29th, 2017, 2:06 pm
by kyriakos876
Right.... "Insert" is the world I couldn't find.
Re: How can one get a table's length?
Posted: September 29th, 2017, 2:09 pm
by jsmorley