Advertisement
Aadvert

histeve

Mar 1st, 2012
3,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.85 KB | None | 0 0
  1. -- A very basic 'hello, steve' program.
  2.  
  3. local sFile, sName = "histeve.txt", "" -- Our data file's name, and our user's name.
  4. -- We won't worry about finding the right directory to save our data in; we'll just use /
  5.  
  6. if fs.exists(sFile) then -- Open the file if it exists, and get the user's name from it.
  7.     local hRead = assert(fs.open(sFile, "r")) -- assert will error for us if we can't open the file.
  8.     -- the 'h' in the varaible name stands for 'handle', and 'read' is to remind me that I can only read from that handle.
  9.     sName = hRead.readLine() -- Read a line of data from the handle
  10.     hRead.close() -- Close the handle! This is extremely important, as not doing so can cause errors when writin/opening the file.
  11. end
  12.  
  13. if sName == "" then -- If the user's name doesn't exist, ask them for it.
  14.     print("Hello there, what's your name?")
  15.     local sInput = read()
  16.     if sInput ~= "" then
  17.         print("Hi, " .. sInput .. "!")
  18.         sName = sInput
  19.         -- Now, let's save the name.
  20.         hWrite = fs.open(sFile, "w") -- w for write! Note that this will also clear the file.
  21.         -- Note that if the above errors, you'll get an error message somewhere else. Use the example below to catch the error.
  22.         hWrite.write(sName) -- Write the user's name
  23.         hWrite.close() -- Close it; still very important.
  24.  
  25.         print("I'm currently calculating Pi to 1" .. string.rep("0", 20) .. "digits.") -- Banter. :)
  26.         print("I'll see you again soon, " .. sName .. "!")
  27.         return
  28.     else
  29.         print("I'm sorry, I'm not allowed to speak to strangers, stranger.")
  30.         return -- exit :D
  31.     end
  32. end
  33. -- We don't need to use an else here, since we're exiting in both branches of the above if statement.
  34. print("Hi again, " .. sName .. "!\nHow are you doing today?") -- \n is a newline.
  35. read() -- Discard the input, this is just an example, after all :)
  36. print("Damnit! You made me garbagecollect all that work on my Pi digits!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement