It is currently April 24th, 2024, 11:28 am
Discuss the use of Lua in Script measures.
kyriakos876
Posts: 919 Joined: January 30th, 2017, 2:01 am
Location: Greece
Post
by kyriakos876 » September 29th, 2017, 1:27 pm
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?
jsmorley
Developer
Posts: 22629 Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA
Post
by jsmorley » September 29th, 2017, 1:36 pm
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
kyriakos876
Posts: 919 Joined: January 30th, 2017, 2:01 am
Location: Greece
Post
by kyriakos876 » September 29th, 2017, 1:38 pm
jsmorley wrote: Yes.
Code: Select all
DeletedBoxes = {2, 9, 14, 3, 8}
NumberOflistElements = #DeletedBoxes
Knowledge is power. Thanks again
jsmorley
Developer
Posts: 22629 Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA
Post
by jsmorley » September 29th, 2017, 1:40 pm
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
jsmorley
Developer
Posts: 22629 Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA
Post
by jsmorley » September 29th, 2017, 1:42 pm
kyriakos876
Posts: 919 Joined: January 30th, 2017, 2:01 am
Location: Greece
Post
by kyriakos876 » September 29th, 2017, 1:45 pm
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
kyriakos876
Posts: 919 Joined: January 30th, 2017, 2:01 am
Location: Greece
Post
by kyriakos876 » September 29th, 2017, 1:57 pm
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?)
jsmorley
Developer
Posts: 22629 Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA
Post
by jsmorley » September 29th, 2017, 2:04 pm
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)
kyriakos876
Posts: 919 Joined: January 30th, 2017, 2:01 am
Location: Greece
Post
by kyriakos876 » September 29th, 2017, 2:06 pm
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.
jsmorley
Developer
Posts: 22629 Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA
Post
by jsmorley » September 29th, 2017, 2:09 pm