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

Loop Bug in Program

Started by goreae, 17 December 2012 - 12:38 AM
goreae #1
Posted 17 December 2012 - 01:38 AM
I had a really strange bug in my program that causes it to loop through "please insert disk below" and asking for a name. I have no clue what is wrong, and the people on the IRC couldn't help me, so I came here. the strange thing is, It was working fine a few minutes ago, but now, it doesn't work at all.

while true do
  nread = fs.open("disk/#","r")
  n=nread.readLine()
  nread.close()
  n=n+1
  nwrite = fs.open("disk/#","w")
  nwrite.write(n)
  nwrite.close()
  term.clear()
  term.setCursorPos(1,1)
  print("Please Insert Disk Below.")
  sleep(2)
  term.clear()
  term.setCursorPos(1,1)
  print("Enter Newbie's Name:")
  name = read()
  if name == "Terminate" then break end
  Names = fs.list(disk.getMountPath("right"))
  duplicate = 0
  print("Checking for Duplicates")
  for i=1,9999 do
    if Names[i] == name then
	  duplicate = 1
	  print("Name Already Registered.")
	  break
    end
    if Names[i] == nil then   
	  fs.copy("disk/nil","disk/"..name)
	  NewID = fs.open("disk/"..name,"w")
	  NewID.write(n)
	  NewID.close()
	  break
    end
  end
  if not duplicate == 1 then
    print("Printing ID.")
    disk.setLabel("bottom",name)
    fs.copy("disk/"..name,"disk2/"..name)
    print("success!")
    while disk.isPresent("bottom") do
	  term.clear()
	  term.setCursorPos(1,1)
	  print("Please remove your disk.")
	  sleep(1)
    end
  end
end
KaoS #2
Posted 17 December 2012 - 03:05 AM
what happened between "a few minutes ago" and now? have you tried restarting the PC, perhaps you overwrote a global function
remiX #3
Posted 17 December 2012 - 11:22 AM
Does it only not work for this code?
goreae #4
Posted 17 December 2012 - 01:59 PM
I don't know what's happening. This is the only program on the computer, besides the blank floppy on the bottom that is going to be written on and the floppy to the right that has blank programs with one line of number in it.

that post-based title of mine is so true at the moment.
immibis #5
Posted 17 December 2012 - 03:45 PM
I think not has a higher precendence than ==, so "not duplicate == 1" is equivalent to "(not duplicate) == 1", which is never true. "Not equal" is ~= in Lua.
It does the duplicate checking so fast that you don't see it, then skips the "Printing ID"/"Please remove your disk" part because of the broken if statement.
theoriginalbit #6
Posted 17 December 2012 - 03:47 PM
I think not has a higher precendence than ==, so "not duplicate == 1" is equivalent to "(not duplicate) == 1", which is never true. "Not equal" is ~= in Lua.
It does the duplicate checking so fast that you don't see it, then skips the "Printing ID"/"Please remove your disk" part because of the broken if statement.

this is true. the other option to doing duplicate ~= 1 is to do not (duplicate == 1), since () takes higher precedence over not.
both are acceptable usage
goreae #7
Posted 17 December 2012 - 04:34 PM
I didn't know about the ~= symbol, but that fixes it. thanks a lot! I've used not in checks before, and it worked just fine, so from the looks of things, it was bugging out for no reason. thanks again!
theoriginalbit #8
Posted 17 December 2012 - 04:39 PM
I didn't know about the ~= symbol, but that fixes it. thanks a lot! I've used not in checks before, and it worked just fine, so from the looks of things, it was bugging out for no reason. thanks again!

i think it was more luck that it never has bugged out before since as immibis stated the not is calculated before the ==