• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua How to insert multiple arrays into a pre-existing table

Xikini

I whore myself out for likes
Senator
Joined
Nov 17, 2010
Messages
6,834
Solutions
586
Reaction score
5,424
Pre-Existing Table.
Lua:
local t = {
   a = {},
   b = {},
   c = {}
}
How it will look, when I add information into the table..
Lua:
local t = {
   a = {value = {{x, y}, {x, y}, {x, y}}, value2 = {{x, y}, {x, y}, {x, y}}, value3 = {{x, y}, {x, y}, {x, y}} },
   b = {},
   c = {}
}

But how do I add the information into the table?

The original value I can add in like this..
Lua:
t.a.value = {x, y}
Which I think would make the table look like this..
Lua:
local t = {
   a = {value = {{x, y}} },
   b = {},
   c = {}
}
But how do I add additional "{x, y}" 's into that pre-existing array.. without deleteing the previous array?

Do I use table.insert..?
Lua:
if not t.a.value then
   t.a.value = {x, y}
else
   table.insert(t.a.value, {x, y})
end

This seems right.. but just trying to wrap my head around it. :p

If someone can say Aye or Nay, with a small explanation.. That'd be really helpful. :p

Thanks!

Xikini
 
Solution
t.a.value = {x, y}

.value holds an array {x,y} intially, but you also want it to hold an array of multiple {x,y}'s later. It can only do one.

The way you add the initial value is the problem
Lua:
--Adding original Value
t.a.value = {x, y}
--should be
t.a.value = {}
t.a.value[#t.a.value+1] = {x,y}

you can also use table.insert instead of t.a[#t.a.value+1].
I am used to that way (next free index place new array /w x,y)
t.a.value = {x, y}

.value holds an array {x,y} intially, but you also want it to hold an array of multiple {x,y}'s later. It can only do one.

The way you add the initial value is the problem
Lua:
--Adding original Value
t.a.value = {x, y}
--should be
t.a.value = {}
t.a.value[#t.a.value+1] = {x,y}

you can also use table.insert instead of t.a[#t.a.value+1].
I am used to that way (next free index place new array /w x,y)
 
Solution
Back
Top