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

Block replacement program: For loop skipped

Started by ProjectXMark1, 21 March 2015 - 03:46 PM
ProjectXMark1 #1
Posted 21 March 2015 - 04:46 PM
I'm writing a program for a Command Computer, that will let me fill an arbitrary volume of blocks with one block.
The problem I have is that the code that actually does the filling, three nested for loops, is not being run.

Here's the code:

tArgs = {...}
textutils.tabulate(tArgs)
X1 = tArgs[1]
X2 = tArgs[4]
Y1 = tArgs[2]
Y2 = tArgs[5]
Z1 = tArgs[3]
Z2 = tArgs[6]
block = tArgs[7]
if X1 <= X2 then
  lowerX = X1
  upperX = X2
  print(lowerX)
  print(upperX)
else
  lowerX = X2
  upperX = X1
  print(lowerX)
  print(upperX)
end
if Y1 <= Y2 then
  lowerY = Y1
  upperY = Y2
  print(lowerY)
  print(upperY)
else
  lowerY = Y2
  upperY = Y1
  print(lowerY)
  print(upperY)
end
if Z1 <= Z2 then
  lowerZ = Z1
  upperZ = Z2
  print(lowerZ)
  print(upperZ)
else
  lowerZ = Z2
  upperZ = Z1
  print(lowerZ)
  print(upperZ)
end
print("Begin block replacement")
for X = lowerX,upperX,1 do
  print("X+1")
  for Y = lowerY,upperY,1 do
	print("Y+1")
	for Z = lowerZ,upperZ,1 do
	  commands.setBlock(X,Y,Z,block)
	  print("Is a block set?")
	end
  end
end
print("Filling complete")
CrazedProgrammer #2
Posted 21 March 2015 - 05:01 PM
I'm writing a program for a Command Computer, that will let me fill an arbitrary volume of blocks with one block.
The problem I have is that the code that actually does the filling, three nested for loops, is not being run.

Here's the code:

tArgs = {...}
textutils.tabulate(tArgs)
X1 = tArgs[1]
X2 = tArgs[4]
Y1 = tArgs[2]
Y2 = tArgs[5]
Z1 = tArgs[3]
Z2 = tArgs[6]
block = tArgs[7]
if X1 <= X2 then
  lowerX = X1
  upperX = X2
  print(lowerX)
  print(upperX)
else
  lowerX = X2
  upperX = X1
  print(lowerX)
  print(upperX)
end
if Y1 <= Y2 then
  lowerY = Y1
  upperY = Y2
  print(lowerY)
  print(upperY)
else
  lowerY = Y2
  upperY = Y1
  print(lowerY)
  print(upperY)
end
if Z1 <= Z2 then
  lowerZ = Z1
  upperZ = Z2
  print(lowerZ)
  print(upperZ)
else
  lowerZ = Z2
  upperZ = Z1
  print(lowerZ)
  print(upperZ)
end
print("Begin block replacement")
for X = lowerX,upperX,1 do
  print("X+1")
  for Y = lowerY,upperY,1 do
	print("Y+1")
	for Z = lowerZ,upperZ,1 do
	  commands.setBlock(X,Y,Z,block)
	  print("Is a block set?")
	end
  end
end
print("Filling complete")
You forgot to call tonumber(str) while defining the positions (X1, Y1…)
Now the positions are just strings and that's why it won't run.
Replace it with this:

X1 = tonumber(tArgs[1])
X2 = tonumber(tArgs[4])
Y1 = tonumber(tArgs[2])
Y2 = tonumber(tArgs[5])
Z1 = tonumber(tArgs[3])
Z2 = tonumber(tArgs[6])
SquidDev #3
Posted 21 March 2015 - 05:05 PM
Also, I would recommend writing a little helper function to get the min and max to help save code:


local sortArgs(q, w)
  q = tonumber(q)
  w = tonumber(w)
  if q < w then
	return q, w
  else
	return w, q
  end
end
local x1, x2 = sortArgs(tArgs[1], tArgs[4])
local y1, y2 = sortArgs(tArgs[2], tArgs[5])
local z1, z2 = sortArgs(tArgs[3], tArgs[6])
Edited on 21 March 2015 - 04:06 PM
ProjectXMark1 #4
Posted 21 March 2015 - 05:34 PM
–snip–
You forgot to call tonumber(str) while defining the positions (X1, Y1…)
Now the positions are just strings and that's why it won't run.
Replace it with this:

X1 = tonumber(tArgs[1])
X2 = tonumber(tArgs[4])
Y1 = tonumber(tArgs[2])
Y2 = tonumber(tArgs[5])
Z1 = tonumber(tArgs[3])
Z2 = tonumber(tArgs[6])
Changed and it's working now. Thanks!
CrazedProgrammer #5
Posted 21 March 2015 - 05:44 PM
–snip–
You forgot to call tonumber(str) while defining the positions (X1, Y1…)
Now the positions are just strings and that's why it won't run.
Replace it with this:

X1 = tonumber(tArgs[1])
X2 = tonumber(tArgs[4])
Y1 = tonumber(tArgs[2])
Y2 = tonumber(tArgs[5])
Z1 = tonumber(tArgs[3])
Z2 = tonumber(tArgs[6])
Changed and it's working now. Thanks!
No problem :D/>
Just keep in mind that you can't implicity convert between types.