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

Breaking Out Of One Loop Without Breaking Out Of Them All

Started by Dragon53535, 22 September 2013 - 11:07 PM
Dragon53535 #1
Posted 23 September 2013 - 01:07 AM
I'm writing a code that requires a while true do statement inside a repeat statement. I'm using the while true do to wait for a button press for an option posed on the screen, while the repeat is there to let it repeat with a different option. I want to know if I can get out of the while true statement, but STAY in the repeat statement.

Code:
Spoiler

function Newuserschecklist()
local bob = fs.open("Users/Newuserschecklist","r") -- opens a file that holds new usernames the program registered to accept or deny
local filebob = {}
local tag = bob.readLine()
table.insert(filebob,tag)
z = 2
tag = bob.readLine()
if tag == nil then
print("No new users.")
sleep(2.5)
Adminmenu()
else
repeat
term.clear()
term.setCursorPos(1,1)
table.insert(filebob,tag)
local alpha = filebob[z]
print(alpha)
print("1. Accept")
print("2. Decline")
while true do
local sir, henry = os.pullEvent ("char")
if henry == "1" then
local newbob = fs.open("Users/1"..alpha,"r")
local file = newbob.readLine()
local test = {}
repeat
table.insert(test,file)
file = newbob.readLine()
until file == nil
newbob.close()
local chick = fs.open("Users/1"..alpha,"w")
chick.writeLine(test[1])
chick.writeLine("Newbie")
chick.close()
break -- these are the problems, its not working the way i need and keeps getting out of the loop instead of searching for new users again.
elseif henry == "2" then
local newbob = fs.open("Users/1"..alpha,"r")
local file = newbob.readLine()
local test = {}
repeat
table.insert(test,file)
file = newbob.readLine()
until file == nil
newbob.close()
local chick = fs.open("Users/1"..alpha,"w")
chick.writeLine(test[1])
chick.writeLine("Denied")
chick.close()
break -- read above comment
end
end
z = z + 1
tag = bob.readLine()
until line == nil
bob.close()
local guess = fs.open("Users/Newuserschecklist","w")
guess.writeLine("la")
guess.close()
Bob() -- sends back to my intro function that starts the program
end
end

The forums doesn't like that i use tab to organize my code…
LBPHacker #2
Posted 23 September 2013 - 09:39 AM
As far as I know, break breaks only one loop (the one on top of the stack). Yep, I was right.
theoriginalbit #3
Posted 23 September 2013 - 01:40 PM
Since LBPHacker already answered the question, and there is nothing more to be added to it, I'm going to answer this statement :P/>

The forums doesn't like that i use tab to organize my code…
Set your text editor to use 2 spaces instead of a tab, the forums likes that better.
Dragon53535 #4
Posted 23 September 2013 - 02:06 PM
Okay so i'm a moron, the entire reason my code wasn't working wasn't the breaks. I thought it was but the entire reason was at the bottom of the code segment:

tag = bob.readline()
until line == nil
So my code was trying to search for a nil in the first place like line instead of the variable tag which was what was supposed to be there
Though thanks for answering my question, i think i will remember it just in case i think it's the problem at some point in the future.