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

How to search for a word in a variable

Started by BenjiButton, 03 December 2013 - 10:37 AM
BenjiButton #1
Posted 03 December 2013 - 11:37 AM
I have made a computer that gives text to a computer that requests it.

I want to search if it tells it to change the text colour but don't know how to.
Bubba #2
Posted 03 December 2013 - 12:30 PM
I assume that you're sending this text over rednet. If that is indeed the case, I would advise sending tables containing both the text and the color information.

For example, here is the sending computer:

rednet.open("top")
rednet.send(3, {"this is some text", colors.white})

And here is the receiving computer:

rednet.open("top")
local id, text = rednet.receive()
term.setTextColor(text[2])
print(text[1])

I'll leave it to you to figure out how to implement this. Here are a few resources that might help:
Lua-users wiki
Programming in Lua
The ComputerCraft wiki
Edited on 03 December 2013 - 11:33 AM
BenjiButton #3
Posted 03 December 2013 - 02:33 PM
How would I make this work for multiple lines that have different colours on each line?
Bomb Bloke #4
Posted 03 December 2013 - 04:49 PM
You would need to send a flag indicating where each line ends, and when the text has been fully sent.

To add to Bubba's example:

rednet.open("top")
rednet.send(3, {"this is some text", colors.white})
rednet.send(3, {" which changes colour", colors.red,"EOL"})
rednet.send(3, {"as we write it", colors.green})
rednet.send(3, {" to the screen.", colors.blue,"EOF"})

And here is the receiving computer:

rednet.open("top")
while true do
  local id, text = rednet.receive()
  term.setTextColor(text[2])
  write(text[1])
  if text[3] == "EOL" then --#End of Line
	print("")
  elseif text[3] == "EOF" then --#End of File
	print("")
	break
  end
end
Edited by