Log In  

I don't know if this is a bug or feature, but it seems to me that nested loops do not work.

I attempted to use the following code, debugging the text showed that only one loop occurred:

local x=0
local y=0
while(x<16) do
 while(y<16) do
  val=mget(x,y)
  print(val)
  y+=1
 end
 x+=1
end

I don't know if this is intended or a limit of lua, but I was able to work around it with the following code:

local i=0
while(i<256) do
 local x=i%16
 local y=(i-x)/16
 val=mget(x,y)
 print(val)
 i+=1
end
P#10191 2015-04-24 17:22 ( Edited 2015-04-25 14:20)

There's a small bug in the first example -- y is not reset to 0 in the outer loop, and so the second time the inner loop is reached, (y < 16) is already false. Should be:

local x=0
local y=0
while(x<16) do
y=0
while(y<16) do
  val=mget(x,y)
  print(val)
  y+=1
end
x+=1
end

And for anyone new to Lua reading this, the same code could be alternatively expressed with for loops:

for y=0,15 do
  for x=0,15 do
    val=mget(x,y)
    print(val)
  end
end
P#10201 2015-04-25 02:33 ( Edited 2015-04-25 06:33)

Of course! Thanks so much.

P#10207 2015-04-25 10:20 ( Edited 2015-04-25 14:20)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-19 08:22:03 | 0.007s | Q:11