Jump to content




I need some help, concerning tables.


  • You cannot reply to this topic
4 replies to this topic

#1 fun

  • Members
  • 8 posts

Posted 13 April 2013 - 06: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

#2 OmegaVest

  • Members
  • 436 posts

Posted 13 April 2013 - 06: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.

#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 April 2013 - 07:12 AM

Just after the outer loop begins and before the inner loop begins, `saveInfo[i] = {}`

#4 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 13 April 2013 - 07: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


#5 fun

  • Members
  • 8 posts

Posted 13 April 2013 - 08:01 AM

Thanks for all the help. I appreciate it.
I'll be able to continue now.

:)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users