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

Compare file to a given value, returning false

Started by WubSt3p, 09 September 2012 - 03:42 PM
WubSt3p #1
Posted 09 September 2012 - 05:42 PM
http://i.imgur.com/8OBkn.png

I've been trying all day to figure this one out, the goal of this particular program is to read in a value from the file userData, check it against the given value 1530, and, if it returns true, send a redstone signal out the back, then run a different program (This is part of a keycard setup I am making on my server)

I've tried the fs.open method shown on the left, the print value shows that it reads 1530 from userData, but that it still checks false against the value 1530.

I have tried this checking against "1530" as well, understanding that fs.readLine() returns a String, but it still checks false.

Is there a different command I should be using, or perhaps should I be doing this with the io.open method? Or is my logic just wrong altogether… any help is appreciated.
ltstingray #2
Posted 09 September 2012 - 06:10 PM
in the future please post the code directly here as I (and I'm sure others) don't want to have to try and type it all out

anyway your if line is… I don't even have words for it…
It should be
if f.readline() == 1530 then

completed and a (slightly) better way to handle the file.

f = fs.open("disk/userData", "r")
line = f.readLine()
f.close()
   if line == 1530 then
      redstone.setOutpur("back", true)
      print("Acess Granted")
      shell.run("/login")
end
Lyqyd #3
Posted 09 September 2012 - 06:13 PM
Does your file look like this?


1530
1530
1530

If not, be aware that it's reading the next line each time you call readLine(), so you'd want something more like this:


local f = fs.open("diskuserData", "r")
if f then
	local code = f.readLine()
	f.close()
	print(code)
	print(code == "1530")
	if code == "1530" then
		rs.setOutput("back", true)
		print("Access Granted")
		shell.run("/login")
	end
end


in the future please post the code directly here as I (and I'm sure others) don't want to have to try and type it all out

anyway your if line is… I don't even have words for it…
It should be
if f.readline() == 1530 then

There was nothing technically wrong with his if statement. The parentheses were unnecessary, but they wouldn't screw anything up.
WubSt3p #4
Posted 09 September 2012 - 06:54 PM
Awesome, feel stupid for not thinking of that one Lyqyd x.x it was my debugging that broke it in the first place, ironic. Thanks for the help everyone :D/>/>