Introduction to Lua Programming
Written By: S Kokila
- 31 Aug 2007 -
Description: Lua is a powerful, light-weight programming language designed for extending applications. Coupled with it being relatively fast and having a very lenient license, it has gained a following among game developers for providing a viable scripting interface. It has been used in games such as World of Warcraft and Far Cry, and in applications such as Adobe Photoshop Lightroom and Snort.
- What is Lua?
- Getting Started
- Identifiers, Types, and Values
- Variables and Expressions
- Operators
- Statements and Assignments
- Control Structures
- File I/O
Basic Input and Output
Lua provides functions for basic input and output through its standard I/O library. The I/O library provides two different styles for file manipulation with implicit and explicit file descriptors. A file descriptor is a handle that allows you to manipulate the file which it corresponds to.
Implicit Descriptors
Lua always assume a default set of file descriptors for I/O: the process's standard input for input, and the process's standard output for output. When you don't specify any file descriptors, the file operations will be performed with these standard default descriptors. As you might have seen in the earlier examples, the call to io.read() reads the data from the process's standard input, typically the keyboard.
You can change the default descriptor for I/O with the io.input() and io.output() functions. These functions take the file name as the argument and from this point any input/output will be to these respective files, until another call is made to io.input() to change the input file or io.output() to change the output file.
-- Implicit Descriptor Example io.write("Enter a number:") -- Writes to process's std output a = io.read() -- Reads from process's std input print("Value of a is", a) -- Always writes to std output io.input("mySqrt.lua") -- Change the input to mySqrt.lua io.output("mySqrtDup.lua") -- Change the output to new file local lineCount = 0 while true do local line = io.read() -- Reads a line from mySqrt.lua if (line == nil) then break end -- Jump out of loop on EOF io.write(line) -- Write to the new file lineCount = lineCount + 1 end print("Number of lines copied =", lineCount)
The io.read() function reads strings from the current input file. Its argument controls what is being read:
"*all" | reads the whole file |
"*line" | reads the next line |
"*number" | reads a number |
num | reads a string with up to num characters |
The call io.read("*all") returns the whole input file from its current position. It returns an empty string if the file is empty or the current position is EOF (end of file).
The call io.read("*line") returns the next line from the file, without the newline character. It returns a nil on EOF. Lua defaults to io.read("*line") when no argument is passed to io.read(). The iterator io.lines() might also be used to read a file line by line.
The io.read("*number") returns a number from the current input file. This is the only case where read returns a number instead of a string. It returns a nil, if it cannot find a number at the current position.
Explicit Descriptors
Explicit descriptors give you the full control on I/O. The central concept in this model is the file handle which represent an open file with a current position. To open a file, you use the io.open() function, that takes the filename and open mode as its argument. The different modes are 'r' for read, 'w' for write and 'a' for append (appending 'b' to the modes, causes the file to be opened for read, write and append in binary mode).
The read and write operations are performed subsequently with the read and write functions, scoped by the file handle using the colon syntax. For example, to open a file and read all its content,
-- Explicit Descriptor/handle f = io.open(filename, "r") if (f ~= nil) then local t = f:read("*all") f:close() else print("Cannot not open", filename) end
With explicit file descriptor method, you can have multiple input files and output files open simultaneously.