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

Go To

Started by Lollerofinni, 07 July 2013 - 12:11 PM
Lollerofinni #1
Posted 07 July 2013 - 02:11 PM
Hello!
I've been wondering that can you in lua, like in the Basic-language that I've learned (a bit)use a command that makes the program jump over lines or go back to the start of the program.

In Basic I would do like this:

Hello: –program name and a specific place in the program
Print "write hello" – prints write hello
Input x – waits for the user to input a variable
if x=hello then end
else goto Hello

In this case the problem could be solved with a loop but it's just an example
Lyqyd #2
Posted 07 July 2013 - 02:17 PM
No, you just use a loop in Lua.
M4sh3dP0t4t03 #3
Posted 07 July 2013 - 02:21 PM
Nope you can't do that, but a you can make functions that can get called later in your program or even inside of itself. Also your program has some syntax errors that I can see.
AgentE382 #4
Posted 07 July 2013 - 03:14 PM
Not in Lua 5.1 (the version CC uses).

In Lua 5.2, you can use the `goto` statement like this:


::Hello:: --program name and a specific place in the program
print"write hello" -- prints write hello
x = io.read() -- waits for the user to input a variable
if x == "hello" then os.exit() else goto Hello end -- NOTE: You can normally have this on two lines,
					-- but it won't work if you are typing it at an interactive Lua prompt.

However, as you stated, it would be easier to use a loop in this circumstance:


repeat
	print"write hello"
	x = io.read()
until x == "hello"
Edited on 07 July 2013 - 03:07 PM
Lyqyd #5
Posted 07 July 2013 - 03:22 PM

	x = io.stdin:read()

Speaking of the version of Lua CC uses, it's easier to just use read(). Unless they added .stdin to ComputerCraft's io library in the beta, it doesn't exist, and io.read() just calls read() after stripping away any arguments passed to it.
AgentE382 #6
Posted 07 July 2013 - 05:08 PM

	x = io.stdin:read()

Speaking of the version of Lua CC uses, it's easier to just use read(). Unless they added .stdin to ComputerCraft's io library in the beta, it doesn't exist, and io.read() just calls read() after stripping away any arguments passed to it.

Sorry 'bout that. I normally code in Lua 5.2.2 and I'm an explicit sort of person. Post edited to fix.
Lollerofinni #7
Posted 08 July 2013 - 04:50 AM
OK Thanks

(And yes I know there is some errors in the basic-code just didn't bother to fix them)