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

Open Modem address from Variable

Started by Sorcelator, 08 April 2013 - 04:00 PM
Sorcelator #1
Posted 08 April 2013 - 06:00 PM
I'm trying to make a little program that allows me to type in a number, which then opens a port on my modem == to that number. This is what I have so far.


while true do
  term.clear()
  term.setCursorPos(1, 1)
  print("Enter Port:")
  local address = read()
  print("Connect to "..address.."? y/n")
  local answer = read()
  if answer == "y" then
    modem.open('..address..')
  end
end

It asks you to enter a port. Then asks you if you want to connect to that port, displaying the port you entered with print() and then if you type Y then it tries to do a modem.open with that variable. The issue is it returns "connect:9: Expected number" if I was to instead do modem.open(…address…) it says "unexpected symbol"

This is mostly an exercise in using variables and modem stuff.
theoriginalbit #2
Posted 08 April 2013 - 06:06 PM
change
modem.open('..address..')
to
modem.open(tonumber(address))
Sorcelator #3
Posted 08 April 2013 - 06:12 PM
ah ok, I take it tonumber converts the data into a "number" where as before it wasn't, even though numbers were the only thing in the variable?
theoriginalbit #4
Posted 08 April 2013 - 06:18 PM
yes tonumber converts to a number. even though as a user you entered a number, read actually returns a string. this is why we need to convert it.
plus you were actually giving a string that said "..address.." to modem.open instead of the address variable.
Sorcelator #5
Posted 08 April 2013 - 06:26 PM
I kind of figured thats what I was doing. I was just running off what I figured out with print(). One more question I have is, is it possible to check to see if the input is a number, or restrict input to a number?
Lyqyd #6
Posted 08 April 2013 - 06:41 PM
tonumber(input) will return nil if input is not a number, so `if tonumber(input) then useMyValidNumber() else askForARealNumber() end`.
Sorcelator #7
Posted 08 April 2013 - 07:41 PM
I have another question kind of along these lines.
Using Variables again, I want to do a basic calculation of distance when moving a Turtle in one direction. I've figured each block forward takes 1 fuel from the Turtle. So if I have a fuel of say 600, I should plan on only using 300 and save the other 300 to get back to where it came from.

So far my code looks like this:

local fuel = turtle.getFuelLevel()
local distance = math.fmod(fuel,2)
print(fuel)
print(distance)
It takes the Fuel Level and then divides it by 2 to get its "distance". By knowing this I can set a hard stop on the turtle by saying if Fuel = Distance then (code to turn around and go home). However, each time I do math.fmod(fuel,2) it returns 0. If I do math.fmod(2,fuel) it gives me 2.
theoriginalbit #8
Posted 08 April 2013 - 07:48 PM
you want to use the / operator. not fmod.
fmod is modulus/modulo, this means that when doing this function the number will always be 0 or 1.


local fuel = turtle.getFuelLevel()
local maxDist = fuel / 2
print( fuel )
print( maxDist )