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

[lua] [Question]

Started by Ginger_Shaggy, 14 January 2013 - 06:33 AM
Ginger_Shaggy #1
Posted 14 January 2013 - 07:33 AM
How do i compare a rednet message to text in a file ?
example :
sender, message = rednet.receive()

if message == (Text in File) then
print (Text in file)
end

any help would be appriciated
thanks
sjele #2
Posted 14 January 2013 - 07:48 AM
You compare the message to the value of whats in the file.
Let's say you have a file called textHere, and that contains compareWithMe
You have already gotten the message over rednet, and it's string is called message


--Firstly we need to get the string from the file
local file = fs.open(textHere, "r") --We ope the file textHere in mode r (Read mode)
textFromFile = file.readLine() --We read the first line from the file, and assign it to textFromFile
file.close() --Allways close the handle after you are done with it

--Now to the comparing part
if message == textFromFile then --Comparing
  print("Yes, the message was equal to what the file had")
else
  print("No message was not equal to what was written in the file")
end
Ginger_Shaggy #3
Posted 14 January 2013 - 08:48 AM
thanks is there also a way to compare a rednet message to a file name ?
remiX #4
Posted 14 January 2013 - 09:04 AM
Yes,


id, msg = rednet.receive()
if fs.exists(msg) then -- checks to see if the msg received matches a file's name
 -- yes
else
 -- no
end
Ginger_Shaggy #5
Posted 16 January 2013 - 11:06 AM
thanks you guys have helped me out alot :)/>
You compare the message to the value of whats in the file. Let's say you have a file called textHere, and that contains compareWithMe You have already gotten the message over rednet, and it's string is called message
 --Firstly we need to get the string from the file local file = fs.open(textHere, "r") --We ope the file textHere in mode r (Read mode) textFromFile = file.readLine() --We read the first line from the file, and assign it to textFromFile file.close() --Allways close the handle after you are done with it --Now to the comparing part if message == textFromFile then --Comparing print("Yes, the message was equal to what the file had") else print("No message was not equal to what was written in the file") end 

is there a way to make it write/read another line ?

Yes,
 id, msg = rednet.receive() if fs.exists(msg) then -- checks to see if the msg received matches a file's name -- yes else -- no end

Thanks for the help