## Tables drive me nuts sometimes: how is this wrong?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
togFox
Party member
Posts: 162
Joined: Sat Jan 30, 2021 9:46 am

### Tables drive me nuts sometimes: how is this wrong?

Code: Select all

local map = {
{0,1,0,1,0},
{0,1,0,1,0},
{0,1,1,1,0},
{0,0,0,0,0},
}

for i,_ in ipairs(map) do
for j, _ in ipairs(map) do
io.write(map[i][j] .. " ")
end
io.write('\n')
end
io.write('\n')
Pretty straight forward? Console says:

Code: Select all

0 1 0 1
0 1 0 1
0 1 1 1
0 0 0 0

Where did the last column go?
togFox
Party member
Posts: 162
Joined: Sat Jan 30, 2021 9:46 am

### Re: Tables drive me nuts sometimes: how is this wrong?

I clearly don't know what I'm doing. Here is another example of something very fundamentally wrong with my thinking:

Code: Select all

	local objMap = {}

for y = 1,10 do
objMap[y] = {}
end

-- instantiate the map with default values
for y = 1,10 do
for x = 1,8 do
print("Instantiating " .. x .. "," .. y)
objMap[x][y] = {}
end
end	
Works as expected:

Code: Select all

Instantiating 1,1
Instantiating 2,1
Instantiating 3,1
[snip]
Instantiating 7,1
Instantiating 8,1
Instantiating 1,2
Instantiating 2,2
[snip]
Instantiating 6,10
Instantiating 7,10
Instantiating 8,10
So I have an 8 x 10 grid? Yes?

Code: Select all

	print("Max y = " .. table.maxn(objMap) .. " max x = " .. table.maxn(objMap[1]))
No - seems not:

Code: Select all

Max y = 10 max x = 10
How?

Going to do some netflix and re-evaluate my life choices.
darkfrei
Party member
Posts: 222
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Tables drive me nuts sometimes: how is this wrong?

Code: Select all

for i, line in ipairs(map) do
for j, value in ipairs(line) do
io.write(value .. " ")
end
io.write('\n')
end
Or

Code: Select all

for i = 1, #map do --#map is 4
local line = map[i]
for j = 1, #line do -- #line is 5
local value = map[i][j]
io.write(value .. " ")
end
io.write('\n')
end
Where line = map[1] was your first line {0,1,0,1,0}.

Next, map 10x8:

Code: Select all

local map = {}
for y = 1, 8 do -- remember, lines!
map[y] = {}
for x = 1, 10 do
print("Instantiating " .. x .. "," .. y)
map[y][x] = {} -- lines are first!
end
end

Or

Code: Select all

local map = {}
for y = 1, 8 do
for x = 1, 10 do
map[x] = map[x] or {} -- if no map[x], then create the empty one
print("Instantiating " .. x .. "," .. y)
map[x][y] = {}
end
end

togFox
Party member
Posts: 162
Joined: Sat Jan 30, 2021 9:46 am

### Re: Tables drive me nuts sometimes: how is this wrong?

I think I'm messing up my y's and x's. I'll take a break and clear my head and start again.
bigbruhh0
Prole
Posts: 17
Joined: Fri Mar 26, 2021 10:36 pm
Location: Russia
Contact:

### Re: Tables drive me nuts sometimes: how is this wrong?

togFox wrote: Sat Apr 10, 2021 3:41 am

Code: Select all

local map = {
{0,1,0,1,0},
{0,1,0,1,0},
{0,1,1,1,0},
{0,0,0,0,0},
}

for i,_ in ipairs(map) do
for j, _ in ipairs(map) do
io.write(map[i][j] .. " ")
end
io.write('\n')
end
io.write('\n')
Pretty straight forward? Console says:

Code: Select all

0 1 0 1
0 1 0 1
0 1 1 1
0 0 0 0

Where did the last column go?
its just because your second cycle is incorrect
you have 4 sections in your "map" table : map[1],map[2],map[3],map[4]
and you are passing through i ={1,2,3,4}, j={1,2,3,4}
so, because of this you are messing 5th column
you should write
for j=1,5
or
for j=1,#map
( if it will work, I'm not sure about second option xD)
or just like this
for j, _ in ipairs(map[ i ]) do
in love with love2d
togFox
Party member
Posts: 162
Joined: Sat Jan 30, 2021 9:46 am

### Re: Tables drive me nuts sometimes: how is this wrong?

Thanks everyone. I stopped calling things x and y because it was mentally screwing me up. I started calling things rows and cols and then everything fell into place instantly.

I'm not sure if this link works but this is a random tiled map with JUMPER correctly pathfinding to the cell (1,1) in the top left corner.

[The red scribble is my leet photoshop skills for demo purposes]

### Who is online

Users browsing this forum: tomxp411 and 27 guests