Catythcatmunchr 0 Posted September 21, 2017 Share Posted September 21, 2017 For some reason break isn't exiting this while loop, when I input "exit" it just prints it and continues the loop instead leaving it. While true do Local input = term.read () Term.write (input) If input == "exit" then Break End End Quote Link to post Share on other sites
1 Solution Molinko 43 Posted September 22, 2017 Solution Share Posted September 22, 2017 I believe term.read returns its input with a newline char (\n) appended to the end of its output. So, typing 'exit' would return 'exit\n'. One option is to use io.read if you don't need all of the options term.read offers. io.read will return the input without a newline. Or, you can strip the newline and compare as usual. local input = term.read() if input:sub(-2) == 'exit' then -- # I believe input:sub(-2) should strip the last two chars (\n) from the input variable... end Quote Link to post Share on other sites
0 Catythcatmunchr 0 Posted September 22, 2017 Author Share Posted September 22, 2017 Thank you, input:sub(-2) didn't work but io.read() did work. I searched through the documentation and you are correct that term.read() adds a newline char. Quote Link to post Share on other sites
0 ben_mkiv 9 Posted September 22, 2017 Share Posted September 22, 2017 '\n' is one character so it may be sub(-1) Quote Link to post Share on other sites
0 Molinko 43 Posted September 22, 2017 Share Posted September 22, 2017 Thanks. It's always referenced as 'the' newline char Quote Link to post Share on other sites
For some reason break isn't exiting this while loop, when I input "exit" it just prints it and continues the loop instead leaving it.
While true do
Local input = term.read ()
Term.write (input)
If input == "exit" then
Break
End
End
Link to post
Share on other sites