3 posts
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
318 posts
Location
Somewhere on the planet called earth
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
3 posts
Posted 14 January 2013 - 08:48 AM
thanks is there also a way to compare a rednet message to a file name ?
2088 posts
Location
South Africa
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
3 posts
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