Log In  

screen={{},{}}

for y = 1,18 do
for x = 1,18 do
screen[x][y] = 1
end
end

Trying to create a 2 dimensional array and fill the grid with a value of 1. But I'm getting and error.

Can anyone help me with this please?

P#140499 2024-01-23 17:17

1

I'd probably do it like this, though I'm sure there are more efficient ways:

    screen={}
    for x = 1,18 do
        add(screen,{})  
        for y = 1,18 do
            add(screen[x],1)
        end
    end
P#140502 2024-01-23 17:42

Thank you!

P#140512 2024-01-23 19:46
1

@2bitchuck
I just checked. It's more efficient to use the index directly when you know what it is, rather than using add. I suspect it's because add requires a length lookup, which gets slightly slower for each item already in the table with an integer index.

As such, this version is faster, though yours is probably better for beginners due to readability.

  screen = {}
  for x = 1,18 do
    screen[x] = {}
    for y = 1,18 do
      screen[x][y] = 1
    end
  end
P#140515 2024-01-23 23:15

[Please log in to post a comment]