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

Rednet Email Program

Started by UP844, 06 March 2013 - 01:31 PM
UP844 #1
Posted 06 March 2013 - 02:31 PM
I'm writing an email program, nothing special. It's actually really simple.

local args = {...}
local id = tonumber (args[1]) or 1
local message = args[2] or "a" -- I don't actually know what I'm suppoed to put for a
rednet.open ("right")
rednet.send (id, message)
I have two main problems:
1.) I need away for the id in line 5 to actually be the variable id, not just nothing that means anything
2.) I need away for the variable message to be able to have spaces, so more like an input type deal.

If anyone can help, it would be greatly appreciated! Please!
nitrogenfingers #2
Posted 06 March 2013 - 02:46 PM
I'm not sure I understand your first question. Passing variable id as a parameter to rednet.send will ensure your message is sent to the first argument passed into the program.

Using command line input to construct your message is possible but inefficient:


--The first word passed through (if there is one)
local message = args[2] or ""
--All subsequent words are appended with a space
for i=3,#args do
  message = message.." "..args[i]
end

A better way to do it is to have the user enter their input within the program, using io.read(). This way you can construct your messages to be as long as you want:


local message = ""
--Read in the first line, if there is one
local nextLine = io.read()
--So long as the user keeps typing lines,
while nextLine ~= "" do
  --I've appended a newline character, but a space may be more appropriate.
  message = message..nextLine.."\n"
  --Get the next line
  nextLine = io.read()
end
--Trims away the extra newline added to the end (there's probably a more elegant way to do this)
message = string.sub(message, 1, #message-1)

To finish typing the message, the user just needs to hit enter after they've finished typing.