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

A machine loop program malfunction

Started by Titan Till, 28 July 2013 - 10:09 PM
Titan Till #1
Posted 29 July 2013 - 12:09 AM
Title: A machine loop program malfunction.

Summary: I am a tekkit veteran, yet just getting into computercraft. I know lua moderately, and I am having trouble with returning a variable from a read() command.

Code:
attempt - 0
while attempt == 0 do
  term.clear()
  term.setCursorPos(1,1)
  print("Industrial Ticker Program") //Yes I go overboard with presentation.
  print("Enter desired ticker time: ")
  loopnum = read()
  tonumber(loopnum)
  if loopnum > 2 then
	print("Are you sure you want ")
	term.setCursorPos(23,3)  //Yes a little inconveinent. So far havent been successful at putting a var inside a print() command.
	print (loopnum," second loops?")
	print("type yes to confrim, or no to undo.")
	local input = read()
	if input == "yes" then
	  attempt = 1
	end
  end
end
Error message: "loop:20: attempt to compare string with number expected, got string."
I understand that it isn't converting the string to a number, if it is the tonumber(loopnum) that is the problem, could I have a quick response and solution?

Many thanks -Titan
AfterLifeLochie #2
Posted 29 July 2013 - 03:45 AM
In response to the PM you sent me:
  • Lua's variables and objects aren't references - with the exception of tables and userdata. The Lua-standard tonumber returns the numerical value of the object passed but doesn't actually do anything with that value - you need to store that value (eg: loopnum = tonumber(loopnum)).
  • Lua uses two periods (..) for concatenation, not commas. As print() only takes one argument (iirc), you need to concatenate loopnum to the string " second loops?" in the print() call.
  • print() will scroll the terminal down one line; write() does not. You can avoid having to set the terminal cursor location by using write() instead.
  • You haven't defined 'attempt' at the start of the script - you've accidentally put a dash and not an equals (attempt - 0). This will cause scope issues for you down the track (namely, unexpected behaviour).