This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The third edition targets Lua 5.2 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.
|Programming in Lua|
|Part I. The Language Chapter 1. Getting Started|
To keep with the tradition,
our first program in Lua just prints
print("Hello World")If you are using the stand-alone Lua interpreter, all you have to do to run your first program is to call the interpreter (usually named
lua) with the name of the text file that contains your program. For instance, if you write the above program in a file
hello.lua, the following command should run it:
prompt> lua hello.lua
As a slightly more complex example, the following program defines a function to compute the factorial of a given number, asks the user for a number, and prints its factorial:
-- defines a factorial function function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end print("enter a number:") a = io.read("*number") -- read a number print(fact(a))
If you are using Lua embedded in an application,
such as CGILua or IUPLua,
you may need to refer to the application manual (or to a "local guru")
to learn how to run your programs.
Nevertheless, Lua is still the same language;
most things that we will see here are valid regardless of how
you are using Lua.
For a start, we recommend that you use the stand-alone interpreter
(that is, the
to run your first examples and experiments.
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|