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?



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

Thank you!


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


[Please log in to post a comment]