It is currently March 28th, 2024, 10:06 pm

How can one get a table's length?

Discuss the use of Lua in Script measures.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

How can one get a table's length?

Post 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?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can one get a table's length?

Post 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
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: How can one get a table's length?

Post by kyriakos876 »

jsmorley wrote:Yes.

Code: Select all

	DeletedBoxes = {2, 9, 14, 3, 8}
	NumberOflistElements = #DeletedBoxes
Knowledge is power. Thanks again :)
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can one get a table's length?

Post 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
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can one get a table's length?

Post by jsmorley »

User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: How can one get a table's length?

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

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
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: How can one get a table's length?

Post 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?)
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can one get a table's length?

Post 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)
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: How can one get a table's length?

Post by kyriakos876 »

Right.... "Insert" is the world I couldn't find.
Last edited by kyriakos876 on September 29th, 2017, 2:09 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can one get a table's length?

Post by jsmorley »