Converting a Table into multiple nested tables?
Posted: Fri May 26, 2017 10:00 pm
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.
Or is there a different and easier way to achieve the same thing...?
Thanks!
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.
This is my current progress - but I cant seem to find a way to insert a value into a table within a table...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
Or is there a different and easier way to achieve the same thing...?
Thanks!