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

Break requiring infinite ends?

Started by mrpoopy345, 14 February 2014 - 07:00 AM
mrpoopy345 #1
Posted 14 February 2014 - 08:00 AM
I was trying to make an antivirus yesterday, for the menu there are (currently) two buttons.
When you click one of the buttons, it does what I want it to do, but because I have it doing

while true do
to test for the pressing of buttons, so if you mis-click the program doesn't stop, that means while it is doing its thing,
(scanning or wiping the computer) It is still testing for button presses in that certain area. The only way around this, I think, is to
break the while true do loop, but whenever I put break, it keeps asking for end's to close the if's that I put.
The Exact error:
Bios:338: [string:"startup"]:103:'end' expected (to close 'if' at line 101)
Code:

--Functions
infectedfiles = 0
j = shell.getRunningProgram()
function scan()
--Keywrods to look for in files
bad = {"os.reboot()", "hacked", "virus", "infected"}
--DO NOT EDIT PAST THIS LINE
gb = 0
badfiles = {}
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.blue)
term.write("Mrpoopy MalScan")
term.setCursorPos(1, 2)
term.setTextColor(colors.yellow)
--Make the dots move!
for i = 1, 2 do
term.write("Scanning for viruses.")
sleep(0.5)
term.clearLine()
term.setCursorPos(1,1)
term.write("Scanning for viruses..")
sleep(0.5)
term.clearLine()
term.setCursorPos(1,1)
term.write("Scanning for viruses...")
sleep(0.5)
term.clearLine()
term.setCursorPos(1,1)
end
--Scan all files, looking for keywords
s = fs.list("/")
for _, v in pairs(s) do
 h = fs.open(v, "r")
 g = h.readAll()
 h.close()
 for i = 1, #bad do
  --If it finds a keyword in the file and the file is not startup
  if string.find(g, bad[i]) and v ~= j then
   --Make sure it doesnt appear twice
   for i = 1, #badfiles do
    if v == badfiles[i] then
     gb = 0
    else
     gb = 1
    end
   end
  if gb == 0 then 
   table.insert(badfiles, v)
   infectedfiles = infectedfiles+1
  end
  end
 end
end
term.setCursorPos(15, 1)
term.setTextColor(colors.black)
print("Infected files:")
term.setTextColor(colors.white)
for i = 1, #badfiles do
 x, y = term.getCursorPos()
 y = y+1
 term.setCursorPos(x, y)
 print(badfiles[i])
end
end
--Clear computer
function clearcomp()
 l = shell.programs()
 s = fs.list("/")
 for _, v in pairs(s) do
  if v ~= j and fs.isDir(v) == false and fs.isReadOnly(v) == false then
   fs.delete(v)
   print(v)
  end
 end
 for _, v in pairs(l) do
  if v ~= j then
  fs.delete(v)
  print(v)
 end
end
end
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(15, 1)
term.setTextColor(colors.lightBlue)
term.write("Mrpoopy MalScan")
term.setCursorPos(14, 4)
term.setBackgroundColor(colors.red)
term.write("                  ")
term.setCursorPos(14, 7)
term.write("                  ")
term.setTextColor(colors.black)
term.setCursorPos(20, 4)
term.write("Scan")
term.setCursorPos(17, 7)
term.write("Wipe computer")
function menu()
while true do
ev, button, x, y = os.pullEvent("mouse_click")
if x > 9 and x < 29 and y > 3 and y < 5 then
 break
 term.clear()
 term.setCursorPos(1,1)
 scan()
 menu()
elseif x > 9 and x < 29 and y > 6 and y < 8 then
 break
 clearcomp()
 menu()
end
end
end
menu()
(Sorry for bad indentation)
I don't know what is happening here since I have used break many time before.
Any help?
Bab #2
Posted 14 February 2014 - 08:08 AM
You currently have a break and then some other code that will not be executed because of the break. I would suggest removing that code and trying again. I'm not sure on this, but I think a break isn't allowed to be followed by anything but an end or else(if).
Edit: According to http://www.lua.org/pil/4.4.html I'm correct in my assumption: returns and breaks must be at the end of a code block.
Edited on 14 February 2014 - 07:11 AM
CometWolf #3
Posted 14 February 2014 - 08:11 AM
What bab's saying is correct, you need to move the 2 functions up above the break, so they execute before the break. Also, put some work into your indentation, it makes it a lot easier for everyone, yourself included.
mrpoopy345 #4
Posted 14 February 2014 - 08:22 AM
Yeah, I have correct indentation, except it is REALLY hard to do indentation on the forums when copying down code…
But thanks for help! This seemed to work great!
Edited on 14 February 2014 - 07:22 AM
awsmazinggenius #5
Posted 14 February 2014 - 09:09 AM
I think you can do this to get a break anywhere (I'm going off the fact that you can do this for returns):

do break end
Bomb Bloke #6
Posted 14 February 2014 - 11:20 AM
Yeah, I have correct indentation, except it is REALLY hard to do indentation on the forums when copying down code…
If you turn off the rich text editor before pasting in your code (the little light switch at the top left of the posting box toggles it), that problem should be sorted.
CometWolf #7
Posted 14 February 2014 - 03:53 PM
I think you can do this to get a break anywhere (I'm going off the fact that you can do this for returns):

do break end
This works, but what the hell is the point? lol
surferpup #8
Posted 17 February 2014 - 05:57 PM
I think you can do this to get a break anywhere (I'm going off the fact that you can do this for returns):

do break end

This works, but what the hell is the point? lol

Actually, do … break … end does not work anywhere, only in loops.
Edited on 17 February 2014 - 04:58 PM
CometWolf #9
Posted 18 February 2014 - 12:10 AM
Why an i qouted in this? What i was saying was, that it works even if it's followed by other code within the same loop. As this was what the OP was having trouble with.
surferpup #10
Posted 18 February 2014 - 01:43 AM
Why an i qouted in this?

Because you are awesome. :D/>

I am just correcting any misconception that a do … break … end will work anywhere – break will only work in a loop.
theoriginalbit #11
Posted 18 February 2014 - 01:47 AM
I am just correcting any misconception that a do … break … end will work anywhere – break will only work in a loop.
Maybe just start pointing out that break and return both act on the parent block and not the `do` block itself, and that the only use of a `do` block is to create a new scope inside of another, when you don't want said parent scope to be able to access variables you place in the `do` block.