Jump to content




need help with making errors output a print string


  • You cannot reply to this topic
7 replies to this topic

#1 cheekycharlie101

  • Members
  • 231 posts

Posted 21 October 2012 - 10:45 AM

ok, so im making a simple program that writes a label to a disk for you.

here is my code:
write("Drive Side: ")
local a = io.read()
write("Disk Name: ")
local B = io.read()
print("writing label to disk")
sleep(2)
disk.setLabel(a, :)/>/>
if disk.setLabel == "false" then
print("Failed to find disk/drive")
else
print("Disk Label Set Successfully")
end
ok, so it will set the label to the disk and everything and will print Disk label set successfully. but when i put in a invalid drive side it just says "disk:3: Invalid Side."
but what i want it to say is that Failed to find disk/drive.
i also noticed on the disk api on the wiki is that the function
disk.setlabel()
returns nil.
so i tried putting
if disk.setLabel == "nil" then
and that did not work either.
can anyone help me out here?
thanks -Cheeky

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 October 2012 - 10:56 AM

Try this (not sure if it will work :)/>), Other people would probably suggest using a table with the valid sides

write("Drive Side: ")
local a = io.read()
if a == "right" or a == "left" or a == "bottom" or a == "above" or a == "front" or a == "back" then else print("Invalid side") sleep (2) os.reboot() end
write("Disk Name: ")
local B = io.read()
print("writing label to disk")
sleep(2)
disk.setLabel(a, :)/>/>
if disk.setLabel == "false" then
print("Failed to find disk/drive")
else
print("Disk Label Set Successfully")
end


#3 ChunLing

  • Members
  • 2,027 posts

Posted 21 October 2012 - 11:08 AM

"if disk.setLabel == "nil" then"
will always return false because disk.setLabel contains a function, not nil, and not the string, "nil"

You want "if not disk.setLabel() then"

But it will still give the "disk:3: Invalid Side." message, the function does that when you call it with an invalid side.

#4 casr144

  • Members
  • 58 posts

Posted 21 October 2012 - 02:21 PM

Can you not check if there is a disk present? I'm not entirely sure about this but wont "if disk.isPresent()" work? Somebody please correct me if I am wrong here.

#5 Lettuce

  • Members
  • 210 posts
  • LocationIn your fridge.

Posted 21 October 2012 - 02:55 PM

That's part of the parser. You can't fix it. If a command doesn't work, it errors, and tells you the line it errored on. The program never runs, so there is no way to make it execute a function. This is also why if you do this:
print(x)
x = "3"
It errors, even though x is declared later.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 21 October 2012 - 04:55 PM

Lettuce's example may just print the word nil, actually.

Look at the peripheral API. It will have functions you can use to see what's on the side of the computer, and if it's a disk drive, what sort of disk (if any) is present.

#7 cheekycharlie101

  • Members
  • 231 posts

Posted 21 October 2012 - 04:56 PM

Thanks, il use all the tips given and try and make my code better and fully working. if i manage it il post a link here to the fully working program.

#8 ChunLing

  • Members
  • 2,027 posts

Posted 21 October 2012 - 07:26 PM

Use the disk.isPresent() function. You should be able to combine this with the commonplace rs.getSides() peripheral checking loop to find whether (and where) a disk is present.
function finddisk()
    for i,v in pairs(rs.getSides()) do    
        if disk.isPresent(v) then return v end
    end
return nil end
This function should check all sides of the computer for a disk, if it finds one it returns the side, if not it returns nil. You could also have it build a table containing all the sides that have a disk, but probably don't need that.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users