This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
fun's profile picture

I need some help, concerning tables.

Started by fun, 13 April 2013 - 05:45 AM
fun #1
Posted 13 April 2013 - 07:45 AM

local saveInfo={
[1]={},
[2]={},
[3]={}
}
for i=1,3 do
for j=1,5 do
  table.insert(saveInfo[i],j,(10*j))
end
end

that works but only if i define the indexes as tables.
like this
[1]={},
[2]={},
[3]={}

I want the code to create tables within that table to store temp data basically. And if there is a way to do it without having to define all the tables that would come in handy.

Thanks in advance
OmegaVest #2
Posted 13 April 2013 - 07:58 AM
You could define them whenever you change i. Last time I did something like this, that worked, but I think you probably will have to define them beforehand, just as you have to define saveInfo as a table beforehand.

Although, if you already have the data stored as a table, you can pass the table, whole, to a specific index, and it should work that way as well.
Lyqyd #3
Posted 13 April 2013 - 08:12 AM
Just after the outer loop begins and before the inner loop begins, `saveInfo = {}`
Kingdaro #4
Posted 13 April 2013 - 08:14 AM
I would just make a function that sets and gets data from the table, and if the table in the table doesn't exist, create it. I also made it so the function also returns values. I use this system a lot; it works well and is very convenient.


local temp = {}

local function data(x, y, value)
  temp[y] = temp[y] or {}
  temp[y][x] = value or temp[y][x]
  return temp[y][x]
end

data(1,1, 'hello')
data(5,3, 'world')

print(data(1,1)) --> hello
fun #5
Posted 13 April 2013 - 09:01 AM
Thanks for all the help. I appreciate it.
I'll be able to continue now.

:)/>