This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Feronzed's profile picture

[Lua][Error]Wierd if-then error

Started by Feronzed, 18 December 2012 - 11:19 AM
Feronzed #1
Posted 18 December 2012 - 12:19 PM
Hello, well, when I was writing this short program:

if turtle then
local size = {...}
if #size < 3 then
   print("Usage: room <length> <height> <depth>")
   return
end
local depth = size[3]
local height = size[2]
local length = size[1]

function digRow()
for h = 1,height do
  for d = 1,depth do
	 while not turtle.forward() do
	    turtle.dig()
	 end
  end
turtle.turnLeft()
turtle.turnLeft()
if h < height then
  turtle.digUp()
  turtle.up()
end
if h = height then
  for d = 1,depth do
    turtle.forward()
  end
  for h = 1,height do
    turtle,down()
  end
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
end
end
for l = 1,length do
digRow()
end
else
print("Mining turtle is required for this program!")
end
This code is meant for digging square hole underground, it should work perfectly, but because of whathever reason it gives me this error:
bios:338: [string "room"]:24: 'then' expected
I checked the code multiple times and couldn't find any mistakes.
I'm sorry if I missed something obvious, and if you need any more informations, please ask.
theoriginalbit #2
Posted 18 December 2012 - 12:23 PM
Hello, well, when I was writing this short program:

if turtle then
local size = {...}
if #size < 3 then
   print("Usage: room <length> <height> <depth>")
   return
end
local depth = size[3]
local height = size[2]
local length = size[1]

function digRow()
for h = 1,height do
  for d = 1,depth do
	 while not turtle.forward() do
		turtle.dig()
	 end
  end
turtle.turnLeft()
turtle.turnLeft()
if h < height then
  turtle.digUp()
  turtle.up()
end
if h = height then
  for d = 1,depth do
	turtle.forward()
  end
  for h = 1,height do
	turtle,down()
  end
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
end
end
for l = 1,length do
digRow()
end
else
print("Mining turtle is required for this program!")
end
This code is meant for digging square hole underground, it should work perfectly, but because of whathever reason it gives me this error:
bios:338: [string "room"]:24: 'then' expected
I checked the code multiple times and couldn't find any mistakes.
I'm sorry if I missed something obvious, and if you need any more informations, please ask.

line 24:

if h = height then

needs to be ==
Feronzed #3
Posted 18 December 2012 - 12:25 PM
Ohhhh… yes…
Didn't saw that.
:wacko:/>
Tnx, btw.
theoriginalbit #4
Posted 18 December 2012 - 12:29 PM
no probs :)/>
Feronzed #5
Posted 19 December 2012 - 07:10 AM
I edited the code a bit:

if turtle then
local size = {...}
if #size < 3 then
   print("Usage: room <length> <height> <depth>")
   return
end
local depth = size[3]
local height = size[2]
local length = size[1]

function digRow()
for h = 1,height do
  for d = 1,depth do
	 while not turtle.forward() do
	    turtle.dig()
	 end
  end
turtle.turnLeft()
turtle.turnLeft()
if h == height then
  for d = 1,depth do
    turtle.forward()
  end
  for h = 1,height do
    turtle.down()
  end
  turtle.turnLeft()
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
else
  while not turtle.up() do
    turtle.digUp()
  end
end
end
end
for l = 1,length do
digRow()
print("Done!")
end
else
print("Mining turtle is required for this program!")
end
The problem is at row 20 where it doesn't recognize variable height. It doesn't give me any error messages, just makes it false and proceeds to else.
But when I put a number there, it works perfectly. (I need variable there)
So, if someone can tell me what am I doing wrong.
Lyqyd #6
Posted 19 December 2012 - 07:37 AM
Try height = tonumber(size[4]), or whichever index height was. Same with your other dimensions.
Feronzed #7
Posted 19 December 2012 - 08:02 AM
Try height = tonumber(size[4]), or whichever index height was. Same with your other dimensions.
Thanks, it worked.
Now it's finaly done.
[Pastebin Link]
ghettodexter #8
Posted 19 December 2012 - 09:38 AM
cool
Feronzed #9
Posted 25 December 2012 - 08:47 AM
No it isn't. XD
With that code there was a major bug (height could only be uneven number), so I wrote a new one, and again as always I'm getting error message at
row 41 to add 'do', which I have there.

if turtle then
local size = {...}
if #size < 3 then
   print("Usage: room <length> <height> <depth>")
   return
end
local depth = tonumber(size[3]) - 1
local height = tonumber(size[2])
local length = tonumber(size[1])
function digRow(e)
for h = 1,height do
  for c = 1,2 do
  for d = 1,depth do
	 while not turtle.forward() do
	    turtle.dig()
	 end
  turtle.turnRight()
  turtle.turnRight()
  end
  end
if h < height then
  while not turtle.up() do
    turtle.digUp()
  end
else
  for h = 1,height do
    while not turtle.down() do
	  turtle.digDown()
    end
  end
  if e == 1 then
  turtle.turnRight()
  while not turtle.forward() do
    turtle.dig()
  end
  turtle.turnLeft()
  else
  turtle.turnLeft()
  for l = 1, length
  while not turtle.forward() do
    turtle.dig()
  end
  end
  turtle.turnRight()
  print("Done!")
  end
end   
end
for l = 1,length do
if l == length then
  digRow(0)
else
  digRow(1)
end
end
else
print("Mining turtle is required for this program!")
end
I was searching for errors for like half an hour and wasn't able to find anything.
Mastake is probably stupid.(As always)
remiX #10
Posted 25 December 2012 - 09:19 AM
  for l = 1, length
  while not turtle.forward() do
    turtle.dig()
  end
  end

the end of this for loop needs a 'do'