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

Writing a program that accepts one number as its argument?

Started by Chaz, 18 May 2014 - 05:09 PM
Chaz #1
Posted 18 May 2014 - 07:09 PM
I'm stepping up my game a bit now, and I'm writing a control rod program for my BigReactors reactor! I'm trying to figure out how to set up the program so that it only accepts one number between 0 and 100 as its argument. So far I've got it set to check that the amount of arguments is exactly one (so that it only accepts one argument):


mon=peripheral.wrap('top')
reactor=peripheral.wrap('BigReactors-Reactor_0')
args = {...}
if #args == 1 and (is a number between 0 and 100) then
	reactor.setControlRodLevel(0, (arg))
	sleep(0.2)
	crn=reactor.getControlRodName(0)
	crl=reactor.getControlRodLevel(0)
	mon.clear()
	mon.setCursorPos(1,1)
	mon.write(crn.." now at "..crl.." percent.")
	sleep(5)
	mon.clear()
else
	mon.clear()
	mon.setCursorPos(1,1)
	mon.write("Must be a number between 0 and 100")
	sleep(5)
	mon.clear()
end

That's how I want the code to work, in theory, but any advice on what I should do to make sure it accepts the argument and passes it on would be appreciated!
Edited on 18 May 2014 - 05:17 PM
wieselkatze #2
Posted 18 May 2014 - 07:40 PM
To check if the argument is a number you could use tonumber().
If it's a valid number, it will pass it as a number else as nil.
This could be done as following:

mon=peripheral.wrap('top')
reactor=peripheral.wrap('BigReactors-Reactor_0')
args = {...}

if tonumber(args[1]) then
  if tonumber(args[1]) > 0 and tonumber(args[1]) < 100 then
    reactor.setControlRodLevel(0, args[1])
    sleep(0.2)
    crn=reactor.getControlRodName(0)
    crl=reactor.getControlRodLevel(0)
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write(crn.." now at "..crl.." percent.")
    sleep(5)
    mon.clear()
  end
end
Chaz #3
Posted 18 May 2014 - 09:10 PM
Thank you for pointing me in the right direction! I had to make a couple of changes to the script (I changed the > and < to >= and <= so that 0 and 100 would be accepted as values too, and I had to change args[1] to tonumber(args[1]) for the setControlRodLevel command as well), and I added in my error handling stuff as well, and everything works smoothly now. Thank you, wieselkatze!

For anyone who would like to try out my control rod program, either check out the code below or type "pastebin get 1sJAmDP3 crod" on your own computer, if you have your own BigReactors reactor you'd like to try this on! (As always, you may need to change the peripheral.wraps so that they're ready for your system setup.)


mon=peripheral.wrap('top')
reactor=peripheral.wrap('BigReactors-Reactor_0')
args = {...}

if tonumber(args[1]) then
  if tonumber(args[1]) >= 0 and tonumber(args[1]) <= 100 then
    reactor.setControlRodLevel(0, tonumber(args[1]))
    sleep(0.2)
    crn=reactor.getControlRodName(0)
    crl=reactor.getControlRodLevel(0)
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write(crn.." now at "..crl.." percent.")
    sleep(5)
    mon.clear()
  else
    mon.clear()
    mon.setCursorPos(1,1)
    mon.write("Must be between 1 and 100!")
    sleep(5)
    mon.clear()
  end
else
mon.clear()
mon.setCursorPos(1,1)
mon.write("Must have ONE numeric value!")
sleep(5)
mon.clear()
end

(EDIT: changed the code a bit so the error reporting can fit on a 3-wide monitor!)
Edited on 18 May 2014 - 07:12 PM
wieselkatze #4
Posted 19 May 2014 - 08:26 AM
Heh, I forgot that one, yes.
Glad that it works now :)/>
Ad for the 0-100 I thought that you really meant *between* 0 and 100 :P/>