Help debugging a code

Hey everyone, I just started coding a few weeks ago. I bought a book that teaches Lua and there is still a lot I don’t understand about it. I typed in a code from the book into Codea that was supposed to make a grid for a simple game board design. When I tried to run it though it told me the lines with “board[i] = {}” and “board[i][j] =0” typed in were a “nil value”. I was wondering why and if there is away to fix this.
P.s. I do know that the code wouldn’t draw anything because it is called in “function setup()” I was mostly looking to see if the code would run without an issue

Here’s the code
(1. function setup()

2.)  local board = {}
3.) local i,j
4.)  local boardW,boardH = 5,5
5.) for i = 1,boardH do
    6.) board[i] = {}

(7. for j=1,boardW do
(8. board[i][j] = 0

9.) end
10.) end
11. ) end

@Progrmr235 Your code worked OK for me. I add code to print the contents of “board”.


function setup()
    local board = {}
    local i,j
    local boardW,boardH = 5,5
    for i = 1,boardH do
        board[i] = {}
        for j=1,boardW do
            board[i][j] = 0
        end
    end
    
    -- print contents of board
    for i=1,5 do
        for j=1,5 do
            print("["..i.."]["..j.."]",board[i][j])
        end
    end
end

@Progrmr235 Here some code just in case your headed in this direction. Tap a square.


displayMode(FULLSCREEN)

function setup()
    board = {}
    boardW,boardH = 5,5
    for i = 1,boardH do
        board[i] = {}
        for j=1,boardW do
            board[i][j] = 0
        end
    end
    size=120
end

function draw()
    background(40,40,50)
    c=0
    for i=1,boardW do
        for j=1,boardH do
            fill(255)
            c=c+1
            if sq==c then
                fill(255,0,0)
            end
            rect(i*size,j*size,size,size)
        end
    end
end

function touched(t)
    c=0
    if t.state==BEGAN then
        for i=1,boardW do
            for j=1,boardH do
                c=c+1
                if t.x>i*size and t.x<i*size+size and
                    t.y>j*size and t.y<j*size+size then
                        sq=c
                end
            end
        end
    end
end

@Progrmr235 Setting board, boardW, and boardH as local in the function setup() means that they can’t be referenced in any other function. If you try to access them in another function, they’ll be “nil”. Is that what you did.

Another funny way :

displayMode(FULLSCREEN)

function setup()
    board = {}
    boardWidth,boardHeight = 5,5
    for li = 1,boardHeight do
        for co = 1,boardWidth do
            board[li .. "," .. co] = 0
        end
    end
    selected = nil
    size = 120
end

function draw()
    background(40,40,50)
    fill(255)
    for locationb,content in pairs(board) do
        pushStyle()
        if content == 1 then fill(255,0,0) end
        li,co = locationb:match("([^,]+),([^,]+)")
        rect(co*size,li*size,size,size)
        popStyle()
    end
end

function touched(touch)
    if touch.state == BEGAN then
        local li,co = math.floor(touch.y/size), math.floor(touch.x/size)
        if li >= 1 and li <= boardHeight and co >= 1 and co <= boardWidth then
            if selected ~= nil then board[selected] = 0 end
            selected = li .. "," .. co
            board[selected] = 1
        end
    end
end

@Progrmr235 - have a look on the wiki link above, there is stacks of info, and I’ve written several ebooks and many tutorial posts you can find here

http://coolcodea.wordpress.com/2013/06/19/index-of-posts/#more-8884

Thanks guys