Page 1 of 1

Initialise manually a table with 2 or 3 dimensions.

Posted: Mon Jul 12, 2021 3:38 pm
by Kitsune64
Hi,

I have a trouble about initialise a table with 2 dimensions...How can I do it manually?

My code:

Code: Select all

	room[1][1]="Tappez commencer ou quitter"
	room[1][2]="alice.png"
	room[2][1]="Il était une fois dans un autre monde..."
	room[2][2]="start.png"
That make an error about room and nil value.

Maybe I will make more dimensions after but for now I want to try like this.

Thanks

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Mon Jul 12, 2021 4:52 pm
by GVovkiv

Code: Select all

room = {
	{
	"Tappez commencer ou quitter",
	"alice.png"
	},
	{
	"Il était une fois dans un autre monde...",
	"start.png"
	},
}
or

Code: Select all

room = {
	[1] = {
    [1] = "Tappez commencer ou quitter",
    [2] = "alice.png"
	},
	[2] = {
    [1] = "Il était une fois dans un autre monde...",
    [2] = "start.png"
	},
}
or

Code: Select all

room = {}
room[1] = {}
	room[1][1] = "Tappez commencer ou quitter"
	room[1][2] = "alice.png"
room[2] = {}
	room[2][1] = "Il était une fois dans un autre monde..."
	room[2][2] = "start.png"
You mean that?

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Mon Jul 12, 2021 4:54 pm
by Kitsune64
Ok I found a solution I must initialize room[1] and room[2] before initialize the others

Code: Select all

room[1]={}
room{2}={}
Don't really know why..

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Mon Jul 12, 2021 4:56 pm
by Kitsune64
Ho yes your solution work! Thanks!

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Mon Jul 12, 2021 4:57 pm
by GVovkiv
Kitsune64 wrote: Mon Jul 12, 2021 4:54 pm Ok I found a solution I must initialize room[1] and room[2] before initialize the others

Code: Select all

room[1]={}
room{2}={}
Don't really know why..
Maybe because before putting values inside table, well, you should create table on first place?

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Mon Jul 12, 2021 5:07 pm
by Kitsune64
Maybe, or it sure...Now that work well. Thanks.

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Tue Jul 27, 2021 6:44 pm
by Jasoco
If you know how big you're gonna want the table to be you can create an empty one easily first:

Code: Select all

my_table = {}
for x = 1, width_of_table do
  my_table[x] = {}
  for y = 1, height_of_table do
    my_table[x][y] = false 
    -- Or whatever other value you want here. I used a boolean for ease of checking later
    -- but you can use an empty string "" or a number 0 or whatever you want to denote that the cell is "empty"
  end
end
Then you'll be able to just replace any table cell in the grid at any time with whatever you want like you do in the OP.

You can keep nesting tables if you want too. However deep you need to go.

Re: Initialise manually a table with 2 or 3 dimensions.

Posted: Tue Jul 27, 2021 6:50 pm
by darkfrei
Jasoco wrote: Tue Jul 27, 2021 6:44 pm If you know how big you're gonna want the table to be you can create an empty one easily first:

Code: Select all

my_table = {}
for x = 1, width_of_table do
  my_table[x] = {}
  for y = 1, height_of_table do
    my_table[x][y] = false 
    -- Or whatever other value you want here. I used a boolean for ease of checking later
    -- but you can use an empty string "" or a number 0 or whatever you want to denote that the cell is "empty"
  end
end
Then you'll be able to just replace any table cell in the grid at any time with whatever you want like you do in the OP.

You can keep nesting tables if you want too. However deep you need to go.
And in other direction:

Code: Select all

map ={}
for x=1,10 do
  for y = 1, 10 do
    map[y] = map[y] or {}
    map[y][x] = math.random()
  end
end