Converting a Table into multiple nested tables?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Mackieram
Prole
Posts: 2
Joined: Fri May 26, 2017 9:45 pm

Converting a Table into multiple nested tables?

Post by Mackieram »

Hi Newbie here,

Apologies if this is simple and has an obvious wiki link I have missed...

I have a table of values I intend to use as a tilemap but first I want to convert it into a group of nested tables such that the number of tables is equal to the map height and the number of values in each table is equal to the map width.

The #original table is 400

and I want to translate that into 20 x 20 tables x values.
function love.load()

farm_tileset = love.graphics.newImage("Maps/farm_tileset.png")

local farm_map_raw = {
22, 22, 14, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 3,
22, 2, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 13,
14, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 13,
9, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 13,
47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 62, 44, 44, 44, 13,
65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 61, 62, 44, 44, 13,
48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 56, 65, 61, 62, 44, 13,
19, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 55, 56, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 57, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 64, 65, 46, 44, 13,
12, 44, 44, 45, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 46, 44, 13,
12, 44, 44, 59, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 56, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
12, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 65, 46, 44, 13,
4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 19, 45, 65, 46, 20, 21
}
print(#farm_map_raw)

local map_height = 20
local map_width = 20
local farm_map = {}
local tile_counter = 0 -- count the number of tiles processed (needed?)
local row_counter = 0 -- count the number of rows processed


for i = 1,map_height do
farm_map[#farm_map + 1] = {rows} --Index insertion method!!! this the number of columns needed to store all the map tiles
for i = 1, map_width do
table.insert(farm_map.rows, 1)
end
end

print(farm_map)

for i,v in ipairs(farm_map) do
print(i,v)
end


end
This is my current progress - but I cant seem to find a way to insert a value into a table within a table...

Or is there a different and easier way to achieve the same thing...?

Thanks!
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Converting a Table into multiple nested tables?

Post by Sir_Silver »

I take it you want to do something like this.

Code: Select all

local map = {}

for x = 1, 20 do
	map[x] = {}
	
	for y = 1, 20 do
		map[x][y] = YourValueHere	
	end
end
Mackieram
Prole
Posts: 2
Joined: Fri May 26, 2017 9:45 pm

Re: Converting a Table into multiple nested tables?

Post by Mackieram »

Yes exactly, thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot], lenlenlL6 and 52 guests