7

I've just started learning Lua, and I'm trying to get the native Lua interpreter to run a program that has been saved to a .txt file. I'm running Windows Vista with Lua 5.1.4.

Perhaps I'm missing something, but the only thing my book (Programming in Lua) says is that all you have to do to run your program is to call the interpreter with the name of the text file that contains your program. Then it gives this supposedly handy piece of code:

% lua filename.lua

Which I can't get to work in cmd or in the Lua interpreter. Further research I've done indicates that I might need to use a

dofile("filename.lua")

command, but I'm not sure how to do this. Specifically, what information do I need to put in the argument? Any help you can give is greatly appreciated.

2
  • Use a different editor, other than notepad. Try notepad++ or vim. The file name needs to literally be "filename.lua", notepad will append a ".txt" onto the end of that like "filename.lua.txt" Commented Sep 28, 2012 at 0:05
  • Thank you for the suggestion. notepad++ at the very least is a much better editor! Commented Sep 28, 2012 at 0:56

1 Answer 1

7

You need to download a Win32 binary (see lua-5.2.1_Win32_bin.zip or lua-5.2.1_Win64_bin.zip here). Unzip this somewhere. How to run your script, in order of easiness and inverse order or common procedure:

  1. Drag your script onto the Lua.exe using the Windows file Explorer.

  2. a. Move your script to the same folder as Lua.exe
    b. Shift right-click on that folder and select Open Command Window Here.
    c. Type lua filename.lua and press Enter.

  3. Add the directory containing Lua.exe to your system PATH, then do steps 2a and 2b on the folder containing your script.

  4. Integrate Lua.exe with your editor in some way (how you do this depends on your editor).
Sign up to request clarification or add additional context in comments.

4 Comments

Okay, just trying to understand: is the Win32 binary is Lua itself or something else? Thank you for the steps and various options, but I can only get 2 to work. 1 only brings up the interpreter for a split second and then it closes. And I don't understand 3 enough to do it, nor will it fit my purposes right now. Is there any information you can give as far as why option 1 isn't working, or how to integrate Lua with Notepad, Notepad++, or SciTE? Thank you for all your help.
Lua is a programming language. It's used in all manner of applications. The lua.exe in the zip file I linked is a very simple Windows command line interpreter for Lua, a no frills way of running Lua code. #1 is running your script, but as soon as the script exits, Lua.exe exits and the window goes away. If you want it to stick around, add the line io.read() to the end of your script, which will force it to wait until you press ENTER to exit. Try the script print('Hello, World!') io.read().
Bingo! I got that interpreter when I installed the batteries included version of Lua. ;) With the io.read() script is works perfectly. For future reference, is there also I way to call the file open from inside the interpreter also?
@rocksrollsrules From the interpreter, you can use dofile(filename) function that you've already found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.