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

How to grab text

Started by diamondpumpkin, 22 April 2015 - 05:31 PM
diamondpumpkin #1
Posted 22 April 2015 - 07:31 PM
I'm trying to make a chat system, but right now when you want to send a message you have to edit the program and type in your message. I don't want to have to do that, I want to know how to grab text from a message. Like say my program to send a message is 'message', I want to add the text for the message to be sent after that. What code would I use to do that?

What I want example: 'message Hello!'
which will send the message "Hello!" to all listening on that rednet channel.

Current sending code:
http://imgur.com/Rpsgiea
Square789 #2
Posted 22 April 2015 - 07:41 PM
I think you want to use args

tArgs = {...}
rednet.broadcast(tArgs[1])

So if you run the program in the shell

> message Hello,all!_
It sends the message "Hello,all!"
But this doesn't work with spaces, so

> message Hello, my name is Steve
would only send "Hello,"
Edited on 22 April 2015 - 05:44 PM
diamondpumpkin #3
Posted 22 April 2015 - 07:45 PM
I think you want to use args

tArgs = {...}
rednet.broadcast(tArgs[1])

Not quite sure how to use that, I'm new at this…

Ah ha! Found out how to use it! Thanks!
diamondpumpkin #4
Posted 22 April 2015 - 08:03 PM
I think you want to use args

tArgs = {...}
rednet.broadcast(tArgs[1])

So if you run the program in the shell

> message Hello,all!_
It sends the message "Hello,all!"
But this doesn't work with spaces, so

> message Hello, my name is Steve
would only send "Hello,"

Just curious, how would I make it work with spaces?
Quintuple Agent #5
Posted 22 April 2015 - 08:04 PM
Spaces seperate arguments, so

> message Hello World
would have tArgs[1] be "Hello" and tArgs[2] be "World" you can recombine these by doing

newMsg = table.concat(tArgs," ")
What this does is take the table tArgs and adds a space in between each argument. The " " is what it will add, so "," would return Hello,World and "-.-" would return Hello-.-World as an example.
Square789 #6
Posted 22 April 2015 - 08:30 PM
I'd do it like this but sure there are more professional ways to do this:

local i = 1
local newMsg = ""
for i = 1, #tArgs do
	 newMsg = newMsg.." "..tostring(tArgs[i])
end
This joins the elements of the table with a space, so

> message Hello, I have 5 million dollars >:-D
outputs this string:

"Hello, I have 5 million dollars >:-D"
And the tosring() is there because otherwise the message entered above would end the program with:
Okay, it works, but some programming languages won't accept joining a string and a number.
HPWebcamAble #7
Posted 23 April 2015 - 01:53 AM
You can put quotes in the command line to tell it to treat it as a string:


> test "this will be a single argument"

In the test program:

args = {...}

print(args[1])  --#> this will be a single argument