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

Trying to make a menu - Lua (end syntax)

Started by Rakshaza, 01 July 2012 - 03:40 AM
Rakshaza #1
Posted 01 July 2012 - 05:40 AM
I'm trying to make a basic menu for an install disk, and it was messing up. My code:

function menu()

print("Welcome to the install disk for Test!")
sleep(2)
print("Would you like to install Test?")

t = read()
if t  == "Yes" then
fs.copy("/disk/test", "/test")
end

if t == "No" then
error()
end

else
menu()
end

menu()

The error:
 bios:206: [string "startup"]:16: 'end' expected (to close 'function' at line 1)

This code is in disk/startup

When I reboot the console with the disk in the drive, the above error occurs.

I made a simpler program to test this out:

function menu()
t = read()
if t == "Yes" then
print("Hello!")
end
if t == "No" then
error()
end
else
menu()
end
menu()

Upon running this program ("readtest"), I get the following error:
 bios:206: [string "startup"]:9: 'end' expected (to close 'function' at line 1)

I'm super new to Lua and programming in general, so any and all help is welcome.


Thanks!
MostlyClueless #2
Posted 01 July 2012 - 05:58 AM
function menu()

print("Welcome to the install disk for Test!")
sleep(2)
print("Would you like to install Test?")

t = read()
if t == "Yes" then
fs.copy("/disk/test", "/test")
print("Installed.")
elseif t == "No" then
print("Not installed.")
else
error()
end
end

menu()

Try the above, it should work fine.

Your issue is the way you set up your 'if' statements. I changed it around to make use of 'elseif' and 'else'. You also tried to call a function from within the same function.
Rakshaza #3
Posted 01 July 2012 - 06:08 AM
Replacing the "error()" in your code with "menu()" still works, though.
ie,

function menu()

print("Welcome to the install disk for Test!")
sleep(2)
print("Would you like to install Test?")

t = read()
if t == "Yes" then
fs.copy("/disk/test", "/test")
print("Installed.")
elseif t == "No" then
print("Not installed.")
else
menu()
end
end

menu()

but it does work now! Thanks :P/>/>
expect to see me on here even more in the future
Rakshaza #4
Posted 01 July 2012 - 06:38 AM
Additional question:

Is there any (easy / not code-intensive) to make the menu requirements (ie, "Yes" or "No") accept similar inputs or not Caps-Sensitive (ie, "yes" or "y", "no" or "no" etc) and perform the same action?

How would you do this?
TechnoColt #5
Posted 01 July 2012 - 07:58 AM
What I usually do is convert them to uppercase strings.


t = read()
t = string.upper(t)
if t == "YES" or t == "Y" then
    print("Hello!")
else
    error()
end


Rakshaza #6
Posted 02 July 2012 - 01:00 AM
Ah, that is helpful. Thank you!

A few more questions (I have them constantly and don't want to make threads for each, so if you would, just keep checking this one :P/>/>)

I'm writing code for a disk/startup to automatically check for connected modem peripherals (and turn them on if present).

I haven't completed it; right now it is just written to handle a no-modem terminal (and alert the user of this fact). I will finish the code later.

My code:

function ptype(str)
t = peripheral.getType(str)
if t == "modem" then
rednet.open(str)
else
print("No modems detected. Terminating.")
end
end

function pcheck(str)
t = peripheral.isPresent(str)
if t == true then
ptype(str)
else
print("No modems detected. Terminating.")
end
end

pcheck("top")
pcheck("bottom")
pcheck("right")
pcheck("left")
pcheck("front")
pcheck("back")

This works (as in, it alerts me that there are no modems present).
However, on bootup, it gives me six "No modems detected. Terminating." My question is: How could I code for there only to be one of these displayed on bootup?

Furthermore, is there some database I can view with all available operators, words, whatever? I'm new to this, and that would help. For example, I didn't even know "elseif" existed.

Thanks!
MysticT #7
Posted 02 July 2012 - 01:30 AM
For the menu, don't use a recursive function, it can crash the computer after some time.
This should work:

local function clear()
  term.clear()
  term.setCursorPos(1, 1)
end

local function menu()
  while true do
    clear()
    print("Welcome to the install disk for Test!")
    print("Would you like to install? (yes / no)")
    local input = string.lower(read())
    if input == "yes" then
	  fs.copy("/disk/test", "/test")
	  break
    elseif input == "no" then
	  break
    end
  end
end

For the modem checking, use this:

local nModems = 0
for _,s in ipairs(rs.getSides()) do
  if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
    rednet.open(s)
    nModems = nModems + 1
  end
end
if nModems == 0 then
  print("No modems found")
end
Rakshaza #8
Posted 02 July 2012 - 02:50 AM
Can you explain how the second line (in the modem check) works code-wise? I've not used those operators before, and I'd like to add onto that code.
MysticT #9
Posted 02 July 2012 - 02:58 AM
rs.getSides() returns a table containing all the valid sides you can use.
With a for loop and the ipairs function you can to iterate over a table.
So, that line iterates over all the valid sides.

For loop syntax:

for var = start, end[, increment] do
end

for key, value in pairs(<table>) do
end

for index, value in ipairs(<table>) do
end

You can read more on the lua manual.
Rakshaza #10
Posted 03 July 2012 - 03:42 AM
local nModems = 0
for _,s in ipairs(rs.getSides()) do
if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
rednet.open(s)
nModems = nModems + 1
end
end
if nModems == 0 then
print("No modems found")
elseif nModems > 0 then
print("Modem found:")
end

This is my code right now, and I'm wondering how to make the code return not only "Modem found" but "Modem found: (top/bottom/etc) ".


print("Modem found:", str)

doesn't work because the string isn't defined outside of the for loop?
I'm not sure why, but how would I go about doing this?
MysticT #11
Posted 03 July 2012 - 03:52 PM
You can add it inside the loop:

local nModems = 0
for _,s in ipairs(rs.getSides()) do
  if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
    rednet.open(s)
    nModems = nModems + 1
    print("Modem found: ", s)
  end
end
if nModems == 0 then
  print("No modems found")
end
makerimages #12
Posted 20 July 2012 - 07:53 AM
i used the code in #3 and when i install, it starts over again, the disk reboots and it asks to install again. How to fix?
Graypup #13
Posted 29 July 2012 - 06:57 PM
Haha, you remove the disk :ph34r:/>/>.