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

ignoring if statement for else

Started by BlueZero, 13 June 2013 - 06:55 PM
BlueZero #1
Posted 13 June 2013 - 08:55 PM
For the most part this program works, but for some reason it's ignoring my first if statement for detecting the startup file and going to the else. I'm not sure if I set up the if statement up properly, but here's my code:


function newCheck()
shell.run("clear")
if fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  lines = ""
  while lines do
   lines = rf.readLine()
   if lines and string.find(lines, [[shell.run("bluelock")]]) then --Completely ignoring this.
	rf.close()
	ef.close()
	return blueLock()  
   else
	print("Preparing for first time run.")
	sleep(2)
	print("Startup File Detected. Adding BlueLock to startup file.\n\nNote: If you have a custom sequence for your startup file BlueLock has been added to the end of the file.\n\nBe sure to place BlueLock's startup opperation in a more appropriate spot in your file.")
	ef.write([[shell.run("bluelock")]].."\n")
	ef.close()
	rf.close()
	sleep(3)
	return blueLock()
   end
  end
elseif not fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  print("Preparing for first time run.")
  sleep(2)
  print("Startup File Not Detected, creating Startup File.")
  ef.write([[shell.run("bluelock")]])
  ef.close()
  sleep(3)
  return blueLock()
end
end

I don't know if there's anything else in this I missed, but so far that's what seems to be causing a problem.
Lyqyd #2
Posted 13 June 2013 - 09:23 PM
Try not opening the same file in two different handles in two different modes at the same time. Open the read handle, then when you're done, close it. Then if you need the append handle, open it, write, and close it again. Don't have both open at once.
BlueZero #3
Posted 13 June 2013 - 09:33 PM
Try not opening the same file in two different handles in two different modes at the same time. Open the read handle, then when you're done, close it. Then if you need the append handle, open it, write, and close it again. Don't have both open at once.

Fixing that seemed to fix my file access problems in trying to delete the startup file, but it's still skipping over the first if and going to my else even if I've got "shell.run("bluelock")" within the startup file.
Lyqyd #4
Posted 13 June 2013 - 09:37 PM
Please post the updated code.
BlueZero #5
Posted 13 June 2013 - 09:42 PM

function newCheck()
shell.run("clear")
if fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  lines = rf.readLine()
  rf.close()
  while lines do
   if lines and string.find(lines, [[shell.run("bluelock")]]) then
    return blueLock()   
   else
    print("Preparing for first time run.")
    sleep(1)
    print("\nStartup File Detected. Adding BlueLock to startup file.\n\nNote: If you have a custom sequence for your startup file BlueLock has been added to the end of the file.\n\nBe sure to place BlueLock's startup opperation in a more appropriate spot in your file.")
    ef.write([[shell.run("bluelock")]].."\n")
    ef.close()
    sleep(3)
    return blueLock()
   end
  end
elseif not fs.exists("startup") then
  local rf = fs.open("startup", "r")
  local ef = fs.open("startup", "a")
  print("Preparing for first time run.\n")
  sleep(1)
  print("Startup File Not Detected, creating Startup File.")
  ef.write([[shell.run("bluelock")]])
  ef.close()
  sleep(3)
  return blueLock()
end
end

I'm guessing this is what you meant. If not, I'm at a loss. Still rather new to the language.
Lyqyd #6
Posted 13 June 2013 - 11:16 PM
You're probably wanting to replace `lines = rf.readLine()` with `lines = rf.readAll()`.
BlueZero #7
Posted 13 June 2013 - 11:30 PM
You're probably wanting to replace `lines = rf.readLine()` with `lines = rf.readAll()`.

Changed that but still the same thing. What seems to happen in the startup file is regardless of shell.run("bluelock") being in there, it still adds to it. If I run it 3 times, there'll be 3 entries of the shell.run command. It's not identifying shell.run being in there, but it's at least identifying that startup exists.

Lol, this one's confusing the hell out of me.
theoriginalbit #8
Posted 13 June 2013 - 11:45 PM
The problem is that string.find requires the use of Lua Patterns so it should be this

string.find(lines, [[shell.run%("bluelock"%)]])
notice the % before each ( ), this is because the brackets in Patterns have a special meaning. Info on Patterns.

EDIT: Also in the else when the startup doesn't exist you don't need this line

local rf = fs.open("startup", "r")
Also all of these

return blueLock()
won't work, I'm assuming in the bluelock file it creates a global function called blueLock, but after you create the startup you don't run either it or the bluelock program meaning that function doesn't exist…
Edited on 13 June 2013 - 09:48 PM
BlueZero #9
Posted 13 June 2013 - 11:52 PM
The problem is that string.find requires the use of Lua Patterns so it should be this

string.find(lines, [[shell.run%("bluelock"%)]])
notice the % before each ( ), this is because the brackets in Patterns have a special meaning. Info on Patterns.

EDIT: Also in the else when the startup doesn't exist you don't need this line

local rf = fs.open("startup", "r")
Also all of these

return blueLock()
won't work, I'm assuming in the bluelock file it creates a global function called blueLock, but after you create the startup you don't run either it or the bluelock program meaning that function doesn't exist…

Ah! That did the trick. My bad. I remember going over this bit before but I guess it didn't stick. Thanks man. My program's almost complete now.