Programming In Lua 3rd Edition Roberto Ierusalimschy
1. Programming In Lua 3rd Edition Roberto
Ierusalimschy download
https://ebookbell.com/product/programming-in-lua-3rd-edition-
roberto-ierusalimschy-4415838
Explore and download more ebooks at ebookbell.com
2. Here are some recommended products that we believe you will be
interested in. You can click the link to download.
Programming In Lua 4th Edition Roberto Ierusalimschy
https://ebookbell.com/product/programming-in-lua-4th-edition-roberto-
ierusalimschy-49119332
Programming In Lua 2nd Edition Roberto Ierusalimschy
https://ebookbell.com/product/programming-in-lua-2nd-edition-roberto-
ierusalimschy-23299570
Programming In Two Semesters Using Python And Java Quentin Charatan
https://ebookbell.com/product/programming-in-two-semesters-using-
python-and-java-quentin-charatan-46494972
Programming In C Volume 2 Composite Data Structures And Modularization
https://ebookbell.com/product/programming-in-c-volume-2-composite-
data-structures-and-modularization-50338946
3. Programming In C Volume 1 Basic Data Structures And Program Statements
Xingni Zhou
https://ebookbell.com/product/programming-in-c-volume-1-basic-data-
structures-and-program-statements-xingni-zhou-50339330
Programming In C Object Oriented Features Laxmisha Rai Editor China
Science Publishing Media Ltd Editor
https://ebookbell.com/product/programming-in-c-object-oriented-
features-laxmisha-rai-editor-china-science-publishing-media-ltd-
editor-51127748
Programming In Visual Basic 2010 8th Edition Bradley Julia Case
https://ebookbell.com/product/programming-in-visual-basic-2010-8th-
edition-bradley-julia-case-55132378
Programming In C For Engineering And Science 1st Edition Nyhoff
https://ebookbell.com/product/programming-in-c-for-engineering-and-
science-1st-edition-nyhoff-55234332
Programming In Java For Beginners Yujing He
https://ebookbell.com/product/programming-in-java-for-beginners-
yujing-he-55591554
9. ProgramminginLua,ThirdEdition
by Roberto Ierusalimschy
ISBN 978-85-903798-5-0
Copyright c 2013, 2003 by Roberto Ierusalimschy. All rights reserved.
The author can be contacted at roberto@lua.org.
Book cover by Luiza Novaes. Lua logo design by Alexandre Nako. Typeset by
the author using L
A
TEX.
Although the author used his best efforts preparing this book, he assumes no
responsibility for errors or omissions, or for any damage that may result from
the use of the information presented here. All product names mentioned in this
book are trademarks of their respective owners.
CIP–BibliotecadoDepartamentodeInformática,PUC-Rio
Ierusalimschy, Roberto
I22 Programming in Lua / Roberto Ierusalimschy. – 3th ed.
– Rio de Janeiro, 2013.
xviii, 348 p. : 25 cm.
Includes index.
ISBN 978-85-903798-5-0
1. Lua (Programming language). I. Title.
005.133 – dc20
Feisty Duck Digital
Book Distribution
www.feistyduck.com
12. Contents
Preface xiii
I The Language 1
1 Getting Started 3
1.1 Chunks 4
1.2 Some Lexical Conventions 5
1.3 Global Variables 6
1.4 The Stand-Alone Interpreter 6
Exercises 8
2 Types and Values 9
2.1 Nil 10
2.2 Booleans 10
2.3 Numbers 10
2.4 Strings 11
2.5 Tables 15
2.6 Functions 18
2.7 Userdata and Threads 18
Exercises 18
3 Expressions 21
3.1 Arithmetic Operators 21
3.2 Relational Operators 22
3.3 Logical Operators 23
3.4 Concatenation 23
3.5 The Length Operator 24
3.6 Precedence 25
3.7 Table Constructors 26
Exercises 28
vii
13. viii Contents
4 Statements 29
4.1 Assignment 29
4.2 Local Variables and Blocks 30
4.3 Control Structures 32
4.4 break, return, and goto 36
Exercises 38
5 Functions 41
5.1 Multiple Results 42
5.2 Variadic Functions 46
5.3 Named Arguments 48
Exercises 49
6 More about Functions 51
6.1 Closures 53
6.2 Non-Global Functions 56
6.3 Proper Tail Calls 58
Exercises 59
7 Iterators and the Generic for 61
7.1 Iterators and Closures 61
7.2 The Semantics of the Generic for 63
7.3 Stateless Iterators 64
7.4 Iterators with Complex State 66
7.5 True Iterators 67
Exercises 68
8 Compilation, Execution, and Errors 71
8.1 Compilation 71
8.2 Precompiled Code 75
8.3 C Code 76
8.4 Errors 77
8.5 Error Handling and Exceptions 79
8.6 Error Messages and Tracebacks 79
Exercises 81
9 Coroutines 83
9.1 Coroutine Basics 83
9.2 Pipes and Filters 86
9.3 Coroutines as Iterators 89
9.4 Non-Preemptive Multithreading 91
Exercises 95
14. ix
10 Complete Examples 97
10.1 The Eight-Queen Puzzle 97
10.2 Most Frequent Words 99
10.3 Markov Chain Algorithm 101
Exercises 102
II Tables and Objects 105
11 Data Structures 107
11.1 Arrays 107
11.2 Matrices and Multi-Dimensional Arrays 108
11.3 Linked Lists 110
11.4 Queues and Double Queues 110
11.5 Sets and Bags 111
11.6 String Buffers 113
11.7 Graphs 114
Exercises 116
12 Data Files and Persistence 117
12.1 Data Files 117
12.2 Serialization 119
Exercises 125
13 Metatables and Metamethods 127
13.1 Arithmetic Metamethods 128
13.2 Relational Metamethods 131
13.3 Library-Defined Metamethods 132
13.4 Table-Access Metamethods 133
Exercises 138
14 The Environment 139
14.1 Global Variables with Dynamic Names 139
14.2 Global-Variable Declarations 141
14.3 Non-Global Environments 142
14.4 Using _ENV 144
14.5 _ENV and load 147
Exercises 148
15 Modules and Packages 151
15.1 The require Function 153
15.2 The Basic Approach for Writing Modules in Lua 156
15.3 Using Environments 158
15.4 Submodules and Packages 159
Exercises 161
15. x Contents
16 Object-Oriented Programming 163
16.1 Classes 165
16.2 Inheritance 166
16.3 Multiple Inheritance 168
16.4 Privacy 170
16.5 The Single-Method Approach 172
Exercises 173
17 Weak Tables and Finalizers 175
17.1 Weak Tables 175
17.2 Memoize Functions 177
17.3 Object Attributes 179
17.4 Revisiting Tables with Default Values 179
17.5 Ephemeron Tables 180
17.6 Finalizers 181
Exercises 185
III The Standard Libraries 187
18 The Mathematical Library 189
Exercises 190
19 The Bitwise Library 191
Exercises 194
20 The Table Library 195
20.1 Insert and Remove 195
20.2 Sort 196
20.3 Concatenation 197
Exercises 198
21 The String Library 199
21.1 Basic String Functions 199
21.2 Pattern-Matching Functions 201
21.3 Patterns 204
21.4 Captures 208
21.5 Replacements 209
21.6 Tricks of the Trade 213
21.7 Unicode 216
Exercises 218
22 The I/O Library 221
22.1 The Simple I/O Model 221
22.2 The Complete I/O Model 224
16. xi
22.3 Other Operations on Files 228
Exercises 229
23 The Operating System Library 231
23.1 Date and Time 231
23.2 Other System Calls 233
Exercises 235
24 The Debug Library 237
24.1 Introspective Facilities 237
24.2 Hooks 242
24.3 Profiles 243
Exercises 245
IV The C API 247
25 An Overview of the C API 249
25.1 A First Example 250
25.2 The Stack 253
25.3 Error Handling with the C API 258
Exercises 260
26 Extending Your Application 261
26.1 The Basics 261
26.2 Table Manipulation 263
26.3 Calling Lua Functions 267
26.4 A Generic Call Function 269
Exercises 270
27 Calling C from Lua 273
27.1 C Functions 273
27.2 Continuations 275
27.3 C Modules 278
Exercises 280
28 Techniques for Writing C Functions 281
28.1 Array Manipulation 281
28.2 String Manipulation 283
28.3 Storing State in C Functions 286
Exercises 292
29 User-Defined Types in C 293
29.1 Userdata 294
29.2 Metatables 296
17. xii Contents
29.3 Object-Oriented Access 299
29.4 Array Access 300
29.5 Light Userdata 301
Exercises 302
30 Managing Resources 305
30.1 A Directory Iterator 305
30.2 An XML Parser 307
Exercises 316
31 Threads and States 319
31.1 Multiple Threads 319
31.2 Lua States 323
Exercises 331
32 Memory Management 333
32.1 The Allocation Function 333
32.2 The Garbage Collector 335
Exercises 338
Index 339
18. Preface
When Waldemar, Luiz, and I started the development of Lua, back in 1993, we
could hardly imagine that it would spread as it did. Started as an in-house
language for two specific projects, currently Lua is widely used in all areas that
can benefit from a simple, extensible, portable, and efficient scripting language,
such as embedded systems, mobile devices, and, of course, games.
We designed Lua, from the beginning, to be integrated with software written
in C/C++ and other conventional languages. This integration brings many
benefits. Lua is a tiny and simple language, partly because it does not try to
do what C is already good for, such as sheer performance, low-level operations,
and interface with third-party software. Lua relies on C for these tasks. What
Lua does offer is what C is not good for: a good distance from the hardware,
dynamic structures, no redundancies, and ease of testing and debugging. For
these goals, Lua has a safe environment, automatic memory management, and
good facilities for handling strings and other kinds of data with dynamic size.
Part of the power of Lua comes from its libraries. This is not by chance. After
all, one of the main strengths of Lua is its extensibility. Many features con-
tribute to this strength. Dynamic typing allows a great degree of polymorphism.
Automatic memory management simplifies interfaces, because there is no need
to decide who is responsible for allocating and deallocating memory, or how to
handle overflows. Higher-order functions and anonymous functions allow a high
degree of parameterization, making functions more versatile.
More than an extensible language, Lua is also a glue language. Lua supports
a component-based approach to software development, where we create an ap-
plication by gluing together existing high-level components. These components
are written in a compiled, statically typed language, such as C or C++; Lua is the
glue that we use to compose and connect these components. Usually, the compo-
nents (or objects) represent more concrete, low-level concepts (such as widgets
and data structures) that are not subject to many changes during program de-
velopment, and that take the bulk of the CPU time of the final program. Lua
gives the final shape of the application, which will probably change a lot dur-
ing the life cycle of the product. However, unlike other glue technologies, Lua
is a full-fledged language as well. Therefore, we can use Lua not only to glue
xiii
19. xiv Preface
components, but also to adapt and reshape them, and to create completely new
components.
Of course, Lua is not the only scripting language around. There are other
languages that you can use for more or less the same purposes. Nevertheless,
Lua offers a set of features that makes it your best choice for many tasks and
gives it a unique profile:
Extensibility: Lua’s extensibility is so remarkable that many people regard Lua
not as a language, but as a kit for building domain-specific languages. We
designed Lua from scratch to be extended, both through Lua code and
through external C code. As a proof of concept, Lua implements most of
its own basic functionality through external libraries. It is really easy to
interface Lua with C/C++, and Lua has been used integrated with several
other languages as well, such as Fortran, Java, Smalltalk, Ada, C#, and
even with other scripting languages, such as Perl and Python.
Simplicity: Lua is a simple and small language. It has few (but powerful)
concepts. This simplicity makes Lua easy to learn and contributes to its
small size. Its complete distribution (source code, manual, plus binaries
for some platforms) fits comfortably in a floppy disk.
Efficiency: Lua has a quite efficient implementation. Independent benchmarks
show Lua as one of the fastest languages in the realm of scripting lan-
guages.
Portability: When we talk about portability, we are talking about running Lua
on all platforms we have ever heard about: all flavors of UNIX and Win-
dows, PlayStation, Xbox, Mac OS X and iOS, Android, Kindle Fire, NOOK,
Haiku, QUALCOMM Brew, IBM mainframes, RISC OS, Symbian OS, Rab-
bit processors, Raspberry Pi, Arduino, and many more. The source code for
each of these platforms is virtually the same. Lua does not use conditional
compilation to adapt its code to different machines; instead, it sticks to
the standard ANSI (ISO) C. This way, you do not usually need to adapt it
to a new environment: if you have an ANSI C compiler, you just have to
compile Lua, out of the box.
Audience
Lua users typically fall into three broad groups: those that use Lua already
embedded in an application program, those that use Lua stand alone, and those
that use Lua and C together.
Many people use Lua embedded in an application program, such as Adobe
Lightroom, Nmap, or World of Warcraft. These applications use the Lua–C API
to register new functions, to create new types, and to change the behavior of
some language operations, configuring Lua for their specific domains. Fre-
quently, the users of such applications do not even know that Lua is an inde-
pendent language adapted for a particular domain. For instance, many devel-
opers of plug-ins for Lightroom do not know about other uses of the language;
20. xv
Nmap users tend to think of Lua as the language of the Nmap Scripting Engine;
players of World of Warcraft may regard Lua as a language exclusive to that
game.
Lua is useful also as a stand-alone language, not only for text-processing and
one-shot little programs, but increasingly for medium-to-large projects, too. For
such uses, the main functionality of Lua comes from libraries. The standard
libraries, for instance, offer basic pattern matching and other functions for
string handling. As Lua improves its support for libraries, there has been a
proliferation of external packages. Lua Rocks, a deployment and management
system for Lua modules, currently features more than 150 packages.
Finally, there are those programmers that work on the other side of the
bench, writing applications that use Lua as a C library. Those people will
program more in C than in Lua, although they need a good understanding of
Lua to create interfaces that are simple, easy to use, and well integrated with
the language.
This book has much to offer to all these people. The first part covers the
language itself, showing how we can explore all its potential. We focus on
different language constructs and use numerous examples and exercises to show
how to use them for practical tasks. Some chapters in this part cover basic
concepts, such as control structures, while others cover topics more advanced,
such as iterators and coroutines.
The second part is entirely devoted to tables, the sole data structure in Lua.
Its chapters discuss data structures, persistence, packages, and object-oriented
programming. There we will unveil the real power of the language.
The third part presents the standard libraries. This part is particularly
useful for those that use Lua as a stand-alone language, although many other
applications also incorporate all or part of the standard libraries. This part
devotes one chapter to each standard library: the mathematical library, the
bitwise library, the table library, the string library, the I/O library, the operating
system library, and the debug library.
Finally, the last part of the book covers the API between Lua and C, for those
that use C to get the full power of Lua. The flavor of this part is necessarily
quite different from the rest of the book. There, we will be programming in C,
not in Lua; therefore, we will be wearing a different hat. For some readers, the
discussion of the C API may be of marginal interest; for others, it may be the
most relevant part of this book.
About the Third Edition
This book is an updated and expanded version of the second edition of Program-
ming in Lua (also known as the PiL 2 book). Although the book structure is
virtually the same, this new edition has substantial new material.
First, I have updated the whole book to Lua 5.2. Of particular relevance is the
chapter about environments, which was mostly rewritten. I also rewrote several
examples to show how to benefit from the new features offered by Lua 5.2.
21. xvi Preface
Nevertheless, I clearly marked the differences from Lua 5.1, so you can use the
book for that version too.
Second, and more important, I have added exercises to all chapters of the
book. These exercises range from simple questions about the language to full
small-size projects. Several exercises illustrate important aspects of program-
ming in Lua and are as important as the examples to expand your toolbox of
useful techniques.
As we did with the first and second editions of Programming in Lua, we self-
published this third edition. Despite the limited marketing, this avenue brings
several benefits: we have total control over the book contents; we keep the full
rights to offer the book in other forms; we have freedom to choose when to release
another edition; and we can ensure that the book does not go out of print.
Other Resources
The reference manual is a must for anyone who wants to really learn a language.
This book does not replace the Lua reference manual. Quite the opposite, they
complement each other. The manual only describes Lua. It shows neither
examples nor a rationale for the constructs of the language. On the other hand,
it describes the whole language; this book skips over seldom-used dark corners of
Lua. Moreover, the manual is the authoritative document about Lua. Wherever
this book disagrees with the manual, trust the manual. To get the manual and
more information about Lua, visit the Lua site at http://www.lua.org.
You can also find useful information at the Lua users’ site, kept by the
community of users at http://lua-users.org. Among other resources, it offers
a tutorial, a list of third-party packages and documentation, and an archive of
the official Lua mailing list.
This book describes Lua 5.2, although most of its contents also apply to
Lua 5.1 and Lua 5.0. The few differences between Lua 5.2 and older Lua 5
versions are clearly marked in the text. If you are using a more recent version
(released after the book), check the corresponding manual for occasional differ-
ences between versions. If you are using a version older than 5.2, this is a good
time to consider an upgrade.
A Few Typographical Conventions
The book encloses “literal strings” between double quotes and single charac-
ters, such as ‘a’, between single quotes. Strings that are used as patterns are
also enclosed between single quotes, like ‘[%w_]*’. The book uses a typewriter
font both for chunks of code and for identifiers. For reservedwords , it uses
a boldface font. Larger chunks of code are shown in display style:
-- program "Hello World"
print("Hello World") --> Hello World
The notation --> shows the output of a statement or, occasionally, the result of
an expression:
22. xvii
print(10) --> 10
13 + 3 --> 16
Because a double hyphen (--) starts a comment in Lua, there is no problem
if you include these annotations in your programs. Finally, the book uses the
notation <--> to indicate that something is equivalent to something else:
this <--> that
Running the Examples
You will need a Lua interpreter to run the examples in this book. Ideally, you
should use Lua 5.2, but most of the examples run also on Lua 5.1 without
modifications.
The Lua site (http://www.lua.org) keeps the source code for the interpreter.
If you have a C compiler and a working knowledge of how to compile C code
in your machine, you should try to install Lua from its source code; it is really
easy. The Lua Binaries site (search for luabinaries) offers precompiled Lua
interpreters for most major platforms. If you use Linux or another UNIX-like
system, you may check the repository of your distribution; several distributions
already offer a package with Lua. For Windows, a good option is Lua for Win-
dows (search for luaforwindows), which is a “batteries-included environment”
for Lua. It includes the interpreter and an integrated text editor, plus many
libraries.
If you are using Lua embedded in an application, such as WoW or Nmap,
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 in this book are valid regardless of how you are using
Lua. Nevertheless, I recommend that you start your study of Lua using the
stand-alone interpreter to run your first examples and experiments.
Acknowledgments
It is almost ten years since I published the first edition of this book. Several
friends and institutions have helped me along this journey.
As always, Luiz Henrique de Figueiredo and Waldemar Celes, Lua coauthors,
offered all kinds of help. André Carregal, Asko Kauppi, Brett Kapilik, Diego
Nehab, Edwin Moragas, Fernando Jefferson, Gavin Wraith, John D. Ramsdell,
and Norman Ramsey provided invaluable suggestions and useful insights for
diverse editions of this book.
Luiza Novaes, head of the Arts & Design Department at PUC-Rio, managed
to find some time in her busy schedule to create an ideal cover for this edition.
Lightning Source, Inc. proved a reliable and efficient option for printing and
distributing the book. Without them, the option of self-publishing the book
would not be an option.
23. xviii Preface
The Center for Latin American Studies at Stanford University provided me
with a much-needed break from regular work, in a stimulating environment,
during which I did most of the work for this third edition.
I also would like to thank the Pontifical Catholic University of Rio de Janeiro
(PUC-Rio) and the Brazilian National Research Council (CNPq) for their contin-
uous support to my work.
Finally, I must express my deep gratitude to Noemi Rodriguez, for all kinds
of help (technical and non-technical) and for illumining my life.
26. 1
Getting Started
To keep with the tradition, our first program in Lua just prints “Hello World”:
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 or lua5.2 — with the
name of the text file that contains your program. If you save the above program
in a file hello.lua, the following command should run it:
% lua hello.lua
As a more complex example, the next 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("*n") -- reads a number
print(fact(a))
3
27. 4 Chapter 1 Getting Started
1.1 Chunks
Each piece of code that Lua executes, such as a file or a single line in interac-
tive mode, is called a chunk. A chunk is simply a sequence of commands (or
statements).
Lua needs no separator between consecutive statements, but you can use
a semicolon if you wish. My personal convention is to use semicolons only to
separate two or more statements written in the same line. Line breaks play no
role in Lua’s syntax; for instance, the following four chunks are all valid and
equivalent:
a = 1
b = a*2
a = 1;
b = a*2;
a = 1; b = a*2
a = 1 b = a*2 -- ugly, but valid
A chunk can be as simple as a single statement, such as in the “Hello World”
example, or it can be composed of a mix of statements and function definitions
(which are actually assignments, as we will see later), such as the factorial
example. A chunk can be as large as you wish. Because Lua is used also as a
data-description language, chunks with several megabytes are not uncommon.
The Lua interpreter has no problems at all with large chunks.
Instead of writing your program to a file, you can run the stand-alone inter-
preter in interactive mode. If you call lua without any arguments, you will get
its prompt:
% lua
Lua 5.2 Copyright (C) 1994-2012 Lua.org, PUC-Rio
>
Thereafter, each command that you type (such as print"Hello World") executes
immediately after you enter it. To exit the interactive mode and the interpreter,
just type the end-of-file control character (ctrl-D in UNIX, ctrl-Z in Windows),
or call the exit function, from the Operating System library — you have to type
os.exit().
In interactive mode, Lua usually interprets each line that you type as a
complete chunk. However, if it detects that the line does not form a complete
chunk, it waits for more input, until it has a complete chunk. This way, you
can enter a multi-line definition, such as the factorial function, directly in
interactive mode. However, it is usually more convenient to put such definitions
in a file and then call Lua to run the file.
You can use the -i option to instruct Lua to start an interactive session after
running the given chunk:
% lua -i prog
28. 1.2 Some Lexical Conventions 5
A command line like this one will run the chunk in file prog and then prompt you
for interaction. This is especially useful for debugging and manual testing. At
the end of this chapter, we will see other options for the stand-alone interpreter.
Another way to run chunks is with the dofile function, which immediately
executes a file. For instance, suppose you have a file lib1.lua with the following
code:
function norm (x, y)
return (x^2 + y^2)^0.5
end
function twice (x)
return 2*x
end
Then, in interactive mode, you can type
> dofile("lib1.lua") -- load your library
> n = norm(3.4, 1.0)
> print(twice(n)) --> 7.0880180586677
The dofile function is useful also when you are testing a piece of code. You
can work with two windows: one is a text editor with your program (in a file
prog.lua, say) and the other is a console running Lua in interactive mode. After
saving a modification in your program, you execute dofile("prog.lua") in the
Lua console to load the new code; then you can exercise the new code, calling its
functions and printing the results.
1.2 Some Lexical Conventions
Identifiers (or names) in Lua can be any string of letters, digits, and underscores,
not beginning with a digit; for instance
i j i10 _ij
aSomewhatLongName _INPUT
You should avoid identifiers starting with an underscore followed by one or more
upper-case letters (e.g., _VERSION); they are reserved for special uses in Lua.
Usually, I reserve the identifier _ (a single underscore) for dummy variables.
In older versions of Lua, the concept of what is a letter depended on the
locale. However, such letters make your program unsuitable to run in systems
that do not support that locale. Lua 5.2 accepts only the ranges A-Z and a-z as
letters to be used in identifiers.
The following words are reserved; we cannot use them as identifiers:
and break do else elseif
end false goto for function
if in local nil not
or repeat return then true
until while
29. 6 Chapter 1 Getting Started
Lua is case-sensitive: and is a reserved word, but And and AND are two other
different identifiers.
A comment starts anywhere with a double hyphen (--) and runs until the
end of the line. Lua also offers block comments, which start with --[[ and run
until the next ]].1
A common trick to comment out a piece of code is to enclose
the code between --[[ and --]], like here:
--[[
print(10) -- no action (commented out)
--]]
To reactivate the code, we add a single hyphen to the first line:
---[[
print(10) --> 10
--]]
In the first example, the --[[ in the first line starts a block comment, and
the double hyphen in the last line is still inside that comment. In the second
example, the sequence ---[[ starts an ordinary, single-line comment, so that
the first and the last lines become independent comments. In this case, the
print is outside comments.
1.3 Global Variables
Global variables do not need declarations; you simply use them. It is not an
error to access a non-initialized variable; you just get the value nil as the result:
print(b) --> nil
b = 10
print(b) --> 10
If you assign nil to a global variable, Lua behaves as if the variable had never
been used:
b = nil
print(b) --> nil
After this assignment, Lua can eventually reclaim the memory used by the
variable.
1.4 The Stand-Alone Interpreter
The stand-alone interpreter (also called lua.c due to its source file, or simply
lua due to its executable) is a small program that allows the direct use of Lua.
This section presents its main options.
1Block comments can be more complex than that, as we will see in Section 2.4.
30. 1.4 The Stand-Alone Interpreter 7
When the interpreter loads a file, it ignores its first line if this line starts
with a hash (‘#’). This feature allows the use of Lua as a script interpreter in
UNIX systems. If you start your script with something like
#!/usr/local/bin/lua
(assuming that the stand-alone interpreter is located at /usr/local/bin), or
#!/usr/bin/env lua
then you can call the script directly, without explicitly calling the Lua inter-
preter.
The usage of lua is
lua [options] [script [args]]
Everything is optional. As we have seen already, when we call lua without
arguments the interpreter enters in interactive mode.
The -e option allows us to enter code directly into the command line, like
here:
% lua -e "print(math.sin(12))" --> -0.53657291800043
(UNIX needs the double quotes to stop the shell from interpreting the parenthe-
ses.)
The -l option loads a library. As we saw previously, -i enters interactive
mode after running the other arguments. Therefore, the next call will load the
lib library, then execute the assignment x=10, and finally present a prompt for
interaction.
% lua -i -llib -e "x = 10"
In interactive mode, you can print the value of any expression by writing a
line that starts with an equal sign followed by the expression:
> = math.sin(3) --> 0.14112000805987
> a = 30
> = a --> 30
This feature helps to use Lua as a calculator.
Before running its arguments, the interpreter looks for an environment vari-
able named LUA_INIT_5_2 or else, if there is no such variable, LUA_INIT. If there
is one of these variables and its content is @filename, then the interpreter runs
the given file. If LUA_INIT_5_2 (or LUA_INIT) is defined but it does not start
with ‘@’, then the interpreter assumes that it contains Lua code and runs it.
LUA_INIT gives us great power when configuring the stand-alone interpreter,
because we have the full power of Lua in the configuration. We can preload
packages, change the path, define our own functions, rename or delete functions,
and so on.
A script can retrieve its arguments in the predefined global variable arg.
In a call like %lua script a b c, the interpreter creates the table arg with all
31. 8 Chapter 1 Getting Started
the command-line arguments, before running the script. The script name goes
into index 0, its first argument (“a” in the example) goes to index 1, and so on.
Preceding options go to negative indices, as they appear before the script. For
instance, consider this call:
% lua -e "sin=math.sin" script a b
The interpreter collects the arguments as follows:
arg[-3] = "lua"
arg[-2] = "-e"
arg[-1] = "sin=math.sin"
arg[0] = "script"
arg[1] = "a"
arg[2] = "b"
More often than not, a script uses only the positive indices (arg[1] and arg[2],
in the example).
Since Lua 5.1, a script can also retrieve its arguments through a vararg ex-
pression. In the main body of a script, the expression ... (three dots) results in
the arguments to the script. (We will discuss vararg expressions in Section 5.2.)
Exercises
Exercise1.1: Run the factorial example. What happens to your program if you
enter a negative number? Modify the example to avoid this problem.
Exercise1.2: Run the twice example, both by loading the file with the -l
option and with dofile. Which way do you prefer?
Exercise1.3: Can you name other languages that use -- for comments?
Exercise1.4: Which of the following strings are valid identifiers?
___ _end End end until? nil NULL
Exercise1.5: Write a simple script that prints its own name without knowing
it in advance.
32. 2
Types and Values
Lua is a dynamically typed language. There are no type definitions in the
language; each value carries its own type.
There are eight basic types in Lua: nil, boolean, number, string, userdata,
function, thread, and table. The type function gives the type name of any given
value:
print(type("Hello world")) --> string
print(type(10.4*3)) --> number
print(type(print)) --> function
print(type(type)) --> function
print(type(true)) --> boolean
print(type(nil)) --> nil
print(type(type(X))) --> string
The last line will result in “string” no matter the value of X, because the result
of type is always a string.
Variables have no predefined types; any variable can contain values of any
type:
print(type(a)) --> nil ('a' is not initialized)
a = 10
print(type(a)) --> number
a = "a string!!"
print(type(a)) --> string
a = print -- yes, this is valid!
a(type(a)) --> function
9
33. 10 Chapter 2 Types and Values
Notice the last two lines: functions are first-class values in Lua; so, we can
manipulate them like any other value. (We will see more about this facility in
Chapter 6.)
Usually, when you use a single variable for different types, the result is
messy code. However, sometimes the judicious use of this facility is helpful,
for instance in the use of nil to differentiate a normal return value from an
abnormal condition.
2.1 Nil
Nil is a type with a single value, nil, whose main property is to be different from
any other value. Lua uses nil as a kind of non-value, to represent the absence
of a useful value. As we have seen, a global variable has a nil value by default,
before its first assignment, and you can assign nil to a global variable to delete
it.
2.2 Booleans
The boolean type has two values, false and true, which represent the traditional
boolean values. However, booleans do not hold a monopoly of condition values:
in Lua, any value can represent a condition. Conditional tests (e.g., conditions in
control structures) consider both the boolean false and nil as false and anything
else as true. In particular, Lua considers both zero and the empty string as true
in conditional tests.
Throughout this book, I will type “false” to mean any false value, that is, the
boolean false or nil. When I mean specifically the boolean value, I will type
“false”. The same holds for “true” and “true”.
2.3 Numbers
The number type represents real (double-precision floating-point) numbers. Lua
has no integer type.
Some people fear that even a simple increment or comparison can go weird
with floating-point numbers. Reality, however, is not like that. Virtually all plat-
forms nowadays follow the IEEE 754 standard for floating-point representation.
Following this standard, the only possible source of errors is a representation er-
ror, which happens when a number cannot be exactly represented. An operation
rounds its result only if that result has no exact representation. Any operation
with a result that has an exact representation must give that exact result.
The fact is that any integer up to 253
(approximately 1016
) has an exact
representation as a double-precision floating-point number. When you use a
double to represent an integer, there is no rounding error at all, unless the
34. 2.4 Strings 11
number has an absolute value greater than 253
. In particular, a Lua number
can represent any 32-bit integer without rounding problems.
Of course, fractional numbers can have representation errors. The situation
here is not different from what happens with pen and paper. If we want to
write 1/7 in decimal, we will have to stop somewhere. If we use ten digits
to represent a number, 1/7 becomes rounded to 0.142857142. If we compute
1/7*7 using ten digits, the result will be 0.999999994, which is different from 1.
Moreover, numbers that have a finite representation in decimal can have an
infinite representation in binary. For instance, 12.7-20+7.3 is not exactly zero
when computed with doubles, because both 12.7 and 7.3 do not have an exact
finite representation in binary (see Exercise 2.3).
Before we go on, remember: integers do have exact representations and
therefore do not have rounding errors.
Most modern CPUs do floating-point arithmetic as fast as (or even faster
than) integer arithmetic. Nevertheless, it is easy to compile Lua so that it
uses another type for numbers, such as longs or single-precision floats. This
is particularly useful for platforms without hardware support for floating point,
such as embedded systems. See file luaconf.h in the distribution for details.
We can write numeric constants with an optional decimal part, plus an
optional decimal exponent. Examples of valid numeric constants are:
4 0.4 4.57e-3 0.3e12 5E+20
Moreover, we can also write hexadecimal constants, prefixing them with 0x.
Since Lua 5.2, hexadecimal constants can also have a fractional part and a
binary exponent (prefixed by ‘p’ or ‘P’), as in the following examples:
0xff (255) 0x1A3 (419) 0x0.2 (0.125) 0x1p-1 (0.5)
0xa.bp2 (42.75)
(For each constant, we added its decimal representation in parentheses.)
2.4 Strings
Strings in Lua have the usual meaning: a sequence of characters. Lua is
eight-bit clean and its strings can contain characters with any numeric code,
including embedded zeros. This means that you can store any binary data into
a string. You can also store Unicode strings in any representation (UTF-8, UTF-
16, etc.). The standard string library that comes with Lua offers no explicit
support for those representations. Nevertheless, we can handle UTF-8 strings
quite reasonably, as we will discuss in Section 21.7.
Strings in Lua are immutable values. You cannot change a character inside
a string, as you can in C; instead, you create a new string with the desired
modifications, as in the next example:
a = "one string"
b = string.gsub(a, "one", "another") -- change string parts
print(a) --> one string
print(b) --> another string
35. 12 Chapter 2 Types and Values
Strings in Lua are subject to automatic memory management, like all other
Lua objects (tables, functions, etc.). This means that you do not have to worry
about allocation and deallocation of strings; Lua handles this for you. A string
can contain a single letter or an entire book. Programs that manipulate strings
with 100K or 1M characters are not unusual in Lua.
You can get the length of a string using the prefix operator ‘#’ (called the
length operator):
a = "hello"
print(#a) --> 5
print(#"good0bye") --> 8
Literal strings
We can delimit literal strings by matching single or double quotes:
a = "a line"
b = 'another line'
They are equivalent; the only difference is that inside each kind of quote you can
use the other quote without escapes.
As a matter of style, most programmers always use the same kind of quotes
for the same kind of strings, where the “kinds” of strings depend on the program.
For instance, a library that manipulates XML may reserve single-quoted strings
for XML fragments, because those fragments often contain double quotes.
Strings in Lua can contain the following C-like escape sequences:
a bell
b back space
f form feed
n newline
r carriage return
t horizontal tab
v vertical tab
backslash
" double quote
' single quote
The following examples illustrate their use:
> print("one linennext linen"in quotes", 'in quotes'")
one line
next line
"in quotes", 'in quotes'
> print('a backslash inside quotes: ''')
a backslash inside quotes: ''
> print("a simpler way: ''")
a simpler way: ''
36. 2.4 Strings 13
We can specify a character in a string also by its numeric value through the
escape sequences ddd and xhh, where ddd is a sequence of up to three dec-
imal digits and hh is a sequence of exactly two hexadecimal digits. As a some-
what complex example, the two literals "alon123"" and '97lo1004923"'
have the same value, in a system using ASCII: 97 is the ASCII code for ‘a’, 10 is
the code for newline, and 49 is the code for the digit ‘1’. (In this example we must
write 49 with three digits, as 049, because it is followed by another digit; other-
wise Lua would read the number as 492.) We can also write that same string as
'x61x6cx6fx0ax31x32x33x22', representing each character by its hex-
adecimal code.
Long strings
We can delimit literal strings also by matching double square brackets, as we
do with long comments. Literals in this bracketed form can run for several
lines and do not interpret escape sequences. Moreover, this form ignores the
first character of the string when this character is a newline. This form is
especially convenient for writing strings that contain large pieces of code, as
in the following example:
page = [[
<html>
<head>
<title>An HTML Page</title>
</head>
<body>
<a href="http://www.lua.org">Lua</a>
</body>
</html>
]]
write(page)
Sometimes, you may want to enclose a piece of code containing something
like a=b[c[i]] (notice the ]] in this code), or you may need to enclose some code
that already has some code commented out. To handle such cases, you can add
any number of equal signs between the two open brackets, as in [===[. After
this change, the literal string ends only at the next closing brackets with the
same number of equal signs in between (]===], in our example). The scanner
ignores pairs of brackets with a different number of equal signs. By choosing an
appropriate number of signs, you can enclose any literal string without having
to add escapes into it.
This same facility is valid for comments, too. For instance, if you start a long
comment with --[=[, it extends until the next ]=]. This facility allows you easily
to comment out a piece of code that contains parts already commented out.
Long strings are the ideal format to include literal text in your code, but you
should not use them for non-text literals. Although literal strings in Lua can
37. 14 Chapter 2 Types and Values
contain arbitrary characters, it is not a good idea to use them in your code: you
may have problems with your text editor; moreover, end-of-line sequences like
“rn” may change to “n” when read. Instead, it is better to code arbitrary bi-
nary data using numeric escape sequences, either in decimal or in hexadecimal,
such as “x13x01xA1xBB”. However, this poses a problem for long strings,
because they would result in quite long lines.
For those situations, Lua 5.2 offers the escape sequence z: it skips all
subsequent characters in the string until the first non-space character. The next
example illustrates its use:
data = "x00x01x02x03x04x05x06x07z
x08x09x0Ax0Bx0Cx0Dx0Ex0F"
The z at the end of the first line skips the following end-of-line and the inden-
tation of the next line, so that the byte x07 is directly followed by x08 in the
resulting string.
Coercions
Lua provides automatic conversions between numbers and strings at run time.
Any numeric operation applied to a string tries to convert the string to a number:
print("10" + 1) --> 11
print("10 + 1") --> 10 + 1
print("-5.3e-10"*"2") --> -1.06e-09
print("hello" + 1) -- ERROR (cannot convert "hello")
Lua applies such coercions not only in arithmetic operators, but also in other
places that expect a number, such as the argument to math.sin.
Conversely, whenever Lua finds a number where it expects a string, it con-
verts the number to a string:
print(10 .. 20) --> 1020
(The .. is the string concatenation operator in Lua. When you write it right
after a numeral, you must separate them with a space; otherwise, Lua thinks
that the first dot is a decimal point.)
Today we are not sure that these automatic coercions were a good idea in
the design of Lua. As a rule, it is better not to count on them. They are handy
in a few places, but add complexity both to the language and to programs that
use them. After all, strings and numbers are different things, despite these
conversions. A comparison like 10=="10" is false, because 10 is a number and
“10” is a string.
If you need to convert a string to a number explicitly, you can use the function
tonumber, which returns nil if the string does not denote a proper number:
38. 2.5 Tables 15
line = io.read() -- read a line
n = tonumber(line) -- try to convert it to a number
if n == nil then
error(line .. " is not a valid number")
else
print(n*2)
end
To convert a number to a string, you can call the function tostring, or
concatenate the number with the empty string:
print(tostring(10) == "10") --> true
print(10 .. "" == "10") --> true
These conversions are always valid.
2.5 Tables
The table type implements associative arrays. An associative array is an array
that can be indexed not only with numbers, but also with strings or any other
value of the language, except nil.
Tables are the main (in fact, the only) data structuring mechanism in Lua,
and a powerful one. We use tables to represent ordinary arrays, sets, records,
and other data structures in a simple, uniform, and efficient way. Lua uses
tables to represent packages and objects as well. When we write io.read, we
think about “the read function from the io module”. For Lua, this expression
means “index the table io using the string read as the key”.
Tables in Lua are neither values nor variables; they are objects. If you are
familiar with arrays in Java or Scheme, then you have a fair idea of what I
mean. You may think of a table as a dynamically allocated object; your program
manipulates only references (or pointers) to them. Lua never does hidden copies
or creation of new tables behind the scenes. Moreover, you do not have to declare
a table in Lua; in fact, there is no way to declare one. You create tables by means
of a constructor expression, which in its simplest form is written as {}:
a = {} -- create a table and store its reference in 'a'
k = "x"
a[k] = 10 -- new entry, with key="x" and value=10
a[20] = "great" -- new entry, with key=20 and value="great"
print(a["x"]) --> 10
k = 20
print(a[k]) --> "great"
a["x"] = a["x"] + 1 -- increments entry "x"
print(a["x"]) --> 11
A table is always anonymous. There is no fixed relationship between a
variable that holds a table and the table itself:
39. 16 Chapter 2 Types and Values
a = {}
a["x"] = 10
b = a -- 'b' refers to the same table as 'a'
print(b["x"]) --> 10
b["x"] = 20
print(a["x"]) --> 20
a = nil -- only 'b' still refers to the table
b = nil -- no references left to the table
When a program has no more references to a table, Lua’s garbage collector will
eventually delete the table and reuse its memory.
Each table can store values with different types of indices, and it grows as
needed to accommodate new entries:
a = {} -- empty table
-- create 1000 new entries
for i = 1, 1000 do a[i] = i*2 end
print(a[9]) --> 18
a["x"] = 10
print(a["x"]) --> 10
print(a["y"]) --> nil
Note the last line: like global variables, table fields evaluate to nil when not
initialized. Also like global variables, you can assign nil to a table field to delete
it. This is not a coincidence: Lua stores global variables in ordinary tables. We
will discuss this subject further in Chapter 14.
To represent records, you use the field name as an index. Lua supports this
representation by providing a.name as syntactic sugar for a["name"]. Therefore,
we could write the last lines of the previous example in a cleaner manner as
follows:
a.x = 10 -- same as a["x"] = 10
print(a.x) -- same as print(a["x"])
print(a.y) -- same as print(a["y"])
For Lua, the two forms are equivalent and can be intermixed freely. For a human
reader, however, each form may signal a different intention. The dot notation
clearly shows that we are using the table as a record, where we have some set of
fixed, predefined keys. The string notation gives the idea that the table can have
any string as a key, and that for some reason we are manipulating that specific
key.
A common mistake for beginners is to confuse a.x with a[x]. The first form
represents a["x"], that is, a table indexed by the string “x”. The second form is
a table indexed by the value of the variable x. See the difference:
a = {}
x = "y"
a[x] = 10 -- put 10 in field "y"
print(a[x]) --> 10 -- value of field "y"
print(a.x) --> nil -- value of field "x" (undefined)
print(a.y) --> 10 -- value of field "y"
40. 2.5 Tables 17
To represent a conventional array or a list, you simply use a table with
integer keys. There is neither a way nor a need to declare a size; you just
initialize the elements you need:
-- read 10 lines, storing them in a table
a = {}
for i = 1, 10 do
a[i] = io.read()
end
Given that you can index a table with any value, you can start the indices of
an array with any number that pleases you. However, it is customary in Lua to
start arrays with one (and not with zero, as in C) and several facilities in Lua
stick to this convention.
Usually, when you manipulate a list you must know its length. It can be a
constant or it can be stored somewhere. Often we store the length of a list in a
non-numeric field of the table; for historical reasons, several programs use the
field “n” for this purpose.
Often, however, the length is implicit. Remember that any non-initialized
index results in nil; you can use this value as a sentinel to mark the end of the
list. For instance, after you read 10 lines into a list, it is easy to know that its
length is 10, because its numeric keys are 1, 2, . . . , 10. This technique only works
when the list does not have holes, which are nil elements inside it. We call such
a list without holes a sequence.
For sequences, Lua offers the length operator ‘#’. It returns the last index, or
the length, of the sequence. For instance, you could print the lines read in the
last example with the following code:
-- print the lines
for i = 1, #a do
print(a[i])
end
Because we can index a table with any type, when indexing a table we have
the same subtleties that arise in equality. Although we can index a table both
with the number 0 and with the string “0”, these two values are different and
therefore denote different entries in a table. Similarly, the strings “+1”, “01”,
and “1” all denote different entries. When in doubt about the actual types of
your indices, use an explicit conversion to be sure:
i = 10; j = "10"; k = "+10"
a = {}
a[i] = "one value"
a[j] = "another value"
a[k] = "yet another value"
print(a[i]) --> one value
print(a[j]) --> another value
print(a[k]) --> yet another value
print(a[tonumber(j)]) --> one value
print(a[tonumber(k)]) --> one value
41. 18 Chapter 2 Types and Values
You can introduce subtle bugs in your program if you do not pay attention to this
point.
2.6 Functions
Functions are first-class values in Lua: programs can store functions in vari-
ables, pass functions as arguments to other functions, and return functions as
results. Such facilities give great flexibility to the language; a program can re-
define a function to add new functionality, or simply erase a function to create
a secure environment when running a piece of untrusted code (such as code re-
ceived through a network). Moreover, Lua offers good support for functional
programming, including nested functions with proper lexical scoping; just wait
until Chapter 6. Finally, first-class functions play a key role in Lua’s object-
oriented facilities, as we will see in Chapter 16.
Lua can call functions written in Lua and functions written in C. Typically,
we resort to C functions both to achieve better performance and to access facili-
ties not easily accessible directly from Lua, such as operating-system facilities.
All the standard libraries in Lua are written in C. They comprise functions for
string manipulation, table manipulation, I/O, access to basic operating system
facilities, mathematical functions, and debugging.
We will discuss Lua functions in Chapter 5 and C functions in Chapter 27.
2.7 Userdata and Threads
The userdata type allows arbitrary C data to be stored in Lua variables. It has
no predefined operations in Lua, except assignment and equality test. Userdata
are used to represent new types created by an application program or a library
written in C; for instance, the standard I/O library uses them to represent open
files. We will discuss more about userdata later, when we get to the C API.
We will explain the thread type in Chapter 9, where we discuss coroutines.
Exercises
Exercise2.1: What is the value of the expression type(nil)==nil? (You can
use Lua to check your answer.) Can you explain this result?
Exercise 2.2: Which of the following are valid numerals? What are their
values?
.0e12 .e12 0.0e 0x12 0xABFG 0xA FFFF 0xFFFFFFFF
0x 0x1P10 0.1e1 0x0.1p1
Exercise 2.3: The number 12.7 is equal to the fraction 127/10, where the
denominator is a power of ten. Can you express it as a common fraction where
the denominator is a power of two? What about the number 5.5?
42. Exercises 19
Exercise2.4: How can you embed the following piece of XML as a string in
Lua?
<![CDATA[
Hello world
]]>
Show at least two different ways.
Exercise2.5: Suppose you need to format a long sequence of arbitrary bytes
as a string literal in Lua. How would you do it? Consider issues like readability,
maximum line length, and performance.
Exercise2.6: Assume the following code:
a = {}; a.a = a
What would be the value of a.a.a.a? Is any a in that sequence somehow
different from the others?
Now, add the next line to the previous code:
a.a.a.a = 3
What would be the value of a.a.a.a now?
44. 3
Expressions
Expressions denote values. Expressions in Lua include the numeric constants
and string literals, variables, unary and binary operations, and function calls.
Expressions include also the unconventional function definitions and table con-
structors.
3.1 Arithmetic Operators
Lua supports the usual arithmetic operators: the binary ‘+’ (addition), ‘-’ (sub-
traction), ‘*’ (multiplication), ‘/’ (division), ‘^’ (exponentiation), ‘%’ (modulo), and
the unary ‘-’ (negation). All of them operate on real numbers. For instance,
x^0.5 computes the square root of x, while x^(-1/3) computes the inverse of its
cubic root.
The following rule defines the modulo operator:
a % b == a - math.floor(a/b)*b
For integer operands, it has the usual meaning, with the result always having
the same sign as the second argument. For real operands, it has some extra
uses. For instance, x%1 is the fractional part of x, and so x-x%1 is its integer
part. Similarly, x-x%0.01 is x with exactly two decimal digits:
x = math.pi
print(x - x%0.01) --> 3.14
As another example of the use of the modulo operator, suppose you want to
check whether a vehicle turning a given angle will start to backtrack. If the
angle is given in degrees, you can use the following formula:
21
45. 22 Chapter 3 Expressions
local tolerance = 10
function isturnback (angle)
angle = angle % 360
return (math.abs(angle - 180) < tolerance)
end
This definition works even for negative angles:
print(isturnback(-180)) --> true
If we want to work with radians instead of degrees, we simply change the
constants in our function:
local tolerance = 0.17
function isturnback (angle)
angle = angle % (2*math.pi)
return (math.abs(angle - math.pi) < tolerance)
end
The operation angle%(2*math.pi) is all we need to normalize any angle to a
value in the interval [0, 2π).
3.2 Relational Operators
Lua provides the following relational operators:
< > <= >= == ~=
All these operators always produce a boolean value.
The == operator tests for equality; the ~= operator is the negation of equality.
We can apply both operators to any two values. If the values have different
types, Lua considers them not equal. Otherwise, Lua compares them according
to their types. Specifically, nil is equal only to itself.
Lua compares tables and userdata by reference, that is, two such values are
considered equal only if they are the very same object. For instance, after the
code
a = {}; a.x = 1; a.y = 0
b = {}; b.x = 1; b.y = 0
c = a
you have a==c but a~=b.
We can apply the order operators only to two numbers or to two strings. Lua
compares strings in alphabetical order, which follows the locale set for Lua. For
instance, with a Portuguese Latin-1 locale, we have "acai"<"açaí"<"acorde".
Values other than numbers and strings can be compared only for equality (and
inequality).
When comparing values with different types, you must be careful: remember
that "0" is different from 0. Moreover, 2<15 is obviously true, but "2"<"15"
is false (alphabetical order). To avoid inconsistent results, Lua raises an error
when you mix strings and numbers in an order comparison, such as 2<"15".
46. 3.3 Logical Operators 23
3.3 Logical Operators
The logical operators are and, or, and not. Like control structures, all logical
operators consider both the boolean false and nil as false, and anything else
as true. The and operator returns its first argument if it is false; otherwise, it
returns its second argument. The or operator returns its first argument if it is
not false; otherwise, it returns its second argument:
print(4 and 5) --> 5
print(nil and 13) --> nil
print(false and 13) --> false
print(4 or 5) --> 4
print(false or 5) --> 5
Both and and or use short-cut evaluation, that is, they evaluate their second
operand only when necessary. Short-cut evaluation ensures that expressions
like (type(v)=="table"and v.tag=="h1") do not cause run-time errors: Lua
will not try to evaluate v.tag when v is not a table.
A useful Lua idiom is x=x or v, which is equivalent to
if not x then x = v end
That is, it sets x to a default value v when x is not set (provided that x is not set
to false).
Another useful idiom is (a and b)or c, or simply a and b or c, because and
has a higher precedence than or. It is equivalent to the C expression a?b:c,
provided that b is not false. For instance, we can select the maximum of two
numbers x and y with a statement like
max = (x > y) and x or y
When x>y, the first expression of the and is true, so the and results in its second
expression (x), which is always true (because it is a number), and then the or
expression results in the value of its first expression, x. When x>y is false, the
and expression is false and so the or results in its second expression, y.
The not operator always returns a boolean value:
print(not nil) --> true
print(not false) --> true
print(not 0) --> false
print(not not 1) --> true
print(not not nil) --> false
3.4 Concatenation
Lua denotes the string concatenation operator by .. (two dots). If any operand
is a number, Lua converts this number to a string. (Some languages use the ‘+’
operator for concatenation, but 3+5 is different from 3..5.)
47. 24 Chapter 3 Expressions
print("Hello " .. "World") --> Hello World
print(0 .. 1) --> 01
print(000 .. 01) --> 01
Remember that strings in Lua are immutable values. The concatenation opera-
tor always creates a new string, without any modification to its operands:
a = "Hello"
print(a .. " World") --> Hello World
print(a) --> Hello
3.5 The Length Operator
The length operator works on strings and tables. On strings, it gives the number
of bytes in the string. On tables, it gives the length of the sequence represented
by the table.
The length operator provides several common Lua idioms for manipulating
sequences:
print(a[#a]) -- prints the last value of sequence 'a'
a[#a] = nil -- removes this last value
a[#a + 1] = v -- appends 'v' to the end of the list
As we saw in the last chapter, the length operator is unpredictable for lists
with holes (nils). It only works for sequences, which we defined as lists without
holes. More precisely, a sequence is a table where the numeric keys comprise a
set 1, . . . , n for some n. (Remember that any key with value nil is actually not in
the table.) In particular, a table with no numeric keys is a sequence with length
zero.
Over the years, there have been many proposals to extend the meaning of the
length operator to lists with holes, but this extension is easier said than done.
The problem is that, because a list is actually a table, the concept of “length”
is somewhat fuzzy. For instance, consider the list resulting from the following
code:
a = {}
a[1] = 1
a[2] = nil -- does nothing, as a[2] is already nil
a[3] = 1
a[4] = 1
It is easy to say that the length of this list is four, and that is has a hole at
index 2. However, what can we say about the next similar example?
a = {}
a[1] = 1
a[10000] = 1
Should we consider a as a list with 10000 elements, where 9998 of them are nil?
Now, the program does this:
48. 3.6 Precedence 25
a[10000] = nil
What is the list length now? Should it be 9999, because the program deleted
the last element? Or maybe still 10000, as the program only changed the last
element to nil? Or should the length collapse to 1?
Another common proposal is to make the # operator return the total number
of elements in the table. This semantics is clear and well defined, but not useful
at all. Consider all previous examples and think how useful would be such
operator for real algorithms over lists or arrays.
Yet more troubling are nils at the end of the list. What should be the length
of the following list?
a = {10, 20, 30, nil, nil}
Remember that, for Lua, a field with nil is indistinct from an absent field.
Therefore, the previous table is equal to {10,20,30}; its length is 3, not 5.
You may consider that a nil at the end of a list is a very special case. However,
many lists are built by adding elements one by one. Any list with holes that was
built that way must have had nils at its end along the way.
Most lists we use in our programs are sequences (e.g., a file line cannot be
nil) and, therefore, most of the time the use of the length operator is safe. If
you really need to handle lists with holes, you should store the length explicitly
somewhere.
3.6 Precedence
Operator precedence in Lua follows the table below, from the higher to the lower
priority:
^
not # - (unary)
* / %
+ -
..
< > <= >= ~= ==
and
or
All binary operators are left associative, except for ‘^’ (exponentiation) and ‘..’
(concatenation), which are right associative. Therefore, the following expres-
sions on the left are equivalent to those on the right:
a+i < b/2+1 <--> (a+i) < ((b/2)+1)
5+x^2*8 <--> 5+((x^2)*8)
a < y and y <= z <--> (a < y) and (y <= z)
-x^2 <--> -(x^2)
x^y^z <--> x^(y^z)
49. 26 Chapter 3 Expressions
When in doubt, always use explicit parentheses. It is easier than looking it up
in the manual and you will probably have the same doubt when you read the
code again.
3.7 Table Constructors
Constructors are expressions that create and initialize tables. They are a dis-
tinctive feature of Lua and one of its most useful and versatile mechanisms.
The simplest constructor is the empty constructor, {}, which creates an
empty table; we have seen it before. Constructors also initialize lists. For
instance, the statement
days = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"}
will initialize days[1] with the string “Sunday” (the first element of the construc-
tor has index 1, not 0), days[2] with “Monday”, and so on:
print(days[4]) --> Wednesday
Lua also offers a special syntax to initialize a table record-like, as in the next
example:
a = {x=10, y=20}
This previous line is equivalent to these commands:
a = {}; a.x=10; a.y=20
The original expression, however, is faster, because Lua creates the table already
with the right size.
No matter what constructor we use to create a table, we can always add fields
to and remove fields from the result:
w = {x=0, y=0, label="console"}
x = {math.sin(0), math.sin(1), math.sin(2)}
w[1] = "another field" -- add key 1 to table 'w'
x.f = w -- add key "f" to table 'x'
print(w["x"]) --> 0
print(w[1]) --> another field
print(x.f[1]) --> another field
w.x = nil -- remove field "x"
However, as I just mentioned, creating a table with a proper constructor is more
efficient, besides being more elegant.
We can mix record-style and list-style initializations in the same constructor:
50. 3.7 Table Constructors 27
polyline = {color="blue",
thickness=2,
npoints=4,
{x=0, y=0}, -- polyline[1]
{x=-10, y=0}, -- polyline[2]
{x=-10, y=1}, -- polyline[3]
{x=0, y=1} -- polyline[4]
}
The above example also illustrates how we can nest constructors to represent
more complex data structures. Each of the elements polyline[i] is a table
representing a record:
print(polyline[2].x) --> -10
print(polyline[4].y) --> 1
Those two constructor forms have their limitations. For instance, you cannot
initialize fields with negative indices, nor with string indices that are not proper
identifiers. For such needs, there is another, more general, format. In this
format, we explicitly write the index to be initialized as an expression, between
square brackets:
opnames = {["+"] = "add", ["-"] = "sub",
["*"] = "mul", ["/"] = "div"}
i = 20; s = "-"
a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s}
print(opnames[s]) --> sub
print(a[22]) --> ---
This syntax is more cumbersome, but more flexible too: both the list-style and
the record-style forms are special cases of this more general syntax. The con-
structor {x=0,y=0} is equivalent to {["x"]=0,["y"]=0}, and the constructor
{"r","g","b"} is equivalent to {[1]="r",[2]="g",[3]="b"}.
You can always put a comma after the last entry. These trailing commas are
optional, but are always valid:
a = {[1]="red", [2]="green", [3]="blue",}
This flexibility frees programs that generate Lua constructors from the need to
handle the last element as a special case.
Finally, you can always use a semicolon instead of a comma in a constructor.
I usually reserve semicolons to delimit different sections in a constructor, for
instance to separate its list part from its record part:
{x=10, y=45; "one", "two", "three"}
51. 28 Chapter 3 Expressions
Exercises
Exercise3.1: What will the following program print?
for i = -10, 10 do
print(i, i % 3)
end
Exercise3.2: What is the result of the expression 2^3^4? What about 2^-3^4?
Exercise3.3: We can represent a polynomial anxn
+ an−1xn−1
+ . . .+ a1x1
+ a0
in Lua as a list of its coefficients, such as {a0,a1,...,an}.
Write a function that receives a polynomial (represented as a table) and a
value for x and returns the polynomial value.
Exercise3.4: Can you write the function from the previous item so that it uses
at most n additions and n multiplications (and no exponentiations)?
Exercise3.5: How can you check whether a value is a boolean without using
the type function?
Exercise3.6: Consider the following expression:
(x and y and (not z)) or ((not y) and x)
Are the parentheses necessary? Would you recommend their use in that expres-
sion?
Exercise3.7: What will the following script print? Explain.
sunday = "monday"; monday = "sunday"
t = {sunday = "monday", [sunday] = monday}
print(t.sunday, t[sunday], t[t.sunday])
Exercise3.8: Suppose that you want to create a table that associates each
escape sequence for strings (see Section 2.4) with its meaning. How could you
write a constructor for that table?
52. 4
Statements
Lua supports an almost conventional set of statements, similar to those in C
or Pascal. The conventional statements include assignment, control structures,
and procedure calls. Lua also supports some not so conventional statements,
such as multiple assignments and local variable declarations.
4.1 Assignment
Assignment is the basic means of changing the value of a variable or a table
field:
a = "hello" .. "world"
t.n = t.n + 1
Lua allows multiple assignment, which assigns a list of values to a list of
variables in one step. Both lists have their elements separated by commas. For
instance, in the assignment
a, b = 10, 2*x
the variable a gets the value 10 and b gets 2*x.
In a multiple assignment, Lua first evaluates all values and only then exe-
cutes the assignments. Therefore, we can use a multiple assignment to swap
two values, as in the following examples:
x, y = y, x -- swap 'x' for 'y'
a[i], a[j] = a[j], a[i] -- swap 'a[i]' for 'a[j]'
29
53. 30 Chapter 4 Statements
Lua always adjusts the number of values to the number of variables: when
the list of values is shorter than the list of variables, the extra variables receive
nil as their values; when the list of values is longer, the extra values are silently
discarded:
a, b, c = 0, 1
print(a, b, c) --> 0 1 nil
a, b = a+1, b+1, b+2 -- value of b+2 is ignored
print(a, b) --> 1 2
a, b, c = 0
print(a, b, c) --> 0 nil nil
The last assignment in the above example shows a common mistake. To initial-
ize a set of variables, you must provide a value for each one:
a, b, c = 0, 0, 0
print(a, b, c) --> 0 0 0
Actually, most of the previous examples are somewhat artificial. I seldom
use multiple assignment simply to write several unrelated assignments in one
line. In particular, a multiple assignment is not faster than its equivalent
single assignments. Nevertheless, often we really need multiple assignment. We
already saw an example, to swap two values. A more frequent use is to collect
multiple returns from function calls. As we will discuss in detail in Section 5.1,
a function call can return multiple values. In such cases, a single expression can
supply the values for several variables. For instance, in the assignment a,b=f()
the call to f returns two results: a gets the first and b gets the second.
4.2 Local Variables and Blocks
Besides global variables, Lua supports local variables. We create local variables
with the local statement:
j = 10 -- global variable
local i = 1 -- local variable
Unlike global variables, local variables have their scope limited to the block
where they are declared. A block is the body of a control structure, the body of a
function, or a chunk (the file or string where the variable is declared):
x = 10
local i = 1 -- local to the chunk
while i <= x do
local x = i*2 -- local to the while body
print(x) --> 2, 4, 6, 8, ...
i = i + 1
end
54. 4.2 Local Variables and Blocks 31
if i > 20 then
local x -- local to the "then" body
x = 20
print(x + 2) -- (would print 22 if test succeeded)
else
print(x) --> 10 (the global one)
end
print(x) --> 10 (the global one)
Beware that this example will not work as expected if you enter it in interactive
mode. In interactive mode, each line is a chunk by itself (unless it is not
a complete command). As soon as you enter the second line of the example
(local i=1), Lua runs it and starts a new chunk in the next line. By then, the
local declaration is already out of scope. To solve this problem, we can delimit
the whole block explicitly, bracketing it with the keywords do–end. Once you
enter the do, the command completes only at the corresponding end, so Lua
does not execute each line by itself.
These do blocks are useful also when you need finer control over the scope of
some local variables:
do
local a2 = 2*a
local d = (b^2 - 4*a*c)^(1/2)
x1 = (-b + d)/a2
x2 = (-b - d)/a2
end -- scope of 'a2' and 'd' ends here
print(x1, x2)
It is good programming style to use local variables whenever possible. Local
variables help you avoid cluttering the global environment with unnecessary
names. Moreover, the access to local variables is faster than to global ones.
Finally, a local variable vanishes as soon as its scope ends, allowing the garbage
collector to release its value.
Lua handles local-variable declarations as statements. As such, you can
write local declarations anywhere you can write a statement. The scope of
the declared variables begins after the declaration and goes until the end of
the block. Each declaration can include an initial assignment, which works the
same way as a conventional assignment: extra values are thrown away; extra
variables get nil. If a declaration has no initial assignment, it initializes all its
variables with nil:
local a, b = 1, 10
if a < b then
print(a) --> 1
local a -- '= nil' is implicit
print(a) --> nil
end -- ends the block started at 'then'
print(a, b) --> 1 10
56. N.J. Formerly Editor of The
Studio and Art Review. Author of
Art for Art’s Sake; History of
Painting; Old English Masters;
&c.
J. C.
W.
James Claude Webster.
Barrister-at-Law, Middle Temple.
Inns of Court.
J. D.
B.
James David Bourchier, M.A., F.R.G.S.
King’s College, Cambridge.
Correspondent of The Times in
South-Eastern Europe.
Commander of the Orders of
Prince Danilo of Montenegro and
of the Saviour of Greece, and
Officer of the Order of St
Alexander of Bulgaria.
Ionian Islands.
J. F.
F.
John Faithfull Fleet, C.I.E., Ph.D.
Commissioner of Central and
Southern Divisions of Bombay,
1891-1897. Author of Inscriptions
of the Early Gupta Kings; &c.
Inscriptions:
Indian.
J. F.-
K.
James Fitzmaurice-Kelly, Litt.D.,
F.R.Hist.S.
Gilmour Professor of Spanish
Language and Literature,
Liverpool University. Norman
McColl Lecturer, Cambridge
University. Fellow of the British
Academy. Member of the Royal
Spanish Academy. Knight
Commander of the Order of
Isla, J. F. de.
57. Alphonso XII. Author of A History
of Spanish Literature; &c.
J. G.
K.
John Graham Kerr, M.A., F.R.S.
Regius Professor of Zoology in
the University of Glasgow.
Formerly Demonstrator in Animal
Morphology in the University of
Cambridge. Fellow of Christ’s
College, Cambridge, 1898-1904.
Walsingham Medallist, 1898. Neill
Prizeman, Royal Society of
Edinburgh, 1904.
Ichthyology (in
part).
J. G.
Sc.
Sir James George Scott, K.C.I.E.
Superintendent and Political
Officer, Southern Shan States.
Author of Burma, a Handbook;
The Upper Burma Gazetteer; &c.
Irrawaddy.
J. H.
A. H.
John Henry Arthur Hart, M.A.
Fellow, Theological Lecturer and
Librarian, St John’s College,
Cambridge.
Hyrcanus.
J. H.
Mu.
John Henry Muirhead, M.A., LL.D.
Professor of Philosophy in the
University of Birmingham. Author
of Elements of Ethics; Philosophy
and Life; &c. Editor of Library of
Philosophy.
Idealism.
J. H.
Be.
Very Rev. John Henry Bernard, M.A.,
D.D., D.C.L.
Dean of St Patrick’s Cathedral,
Dublin. Archbishop King’s
Ireland, Church
of.
58. Professor of Divinity and formerly
Fellow of Trinity College, Dublin.
Joint-editor of the Irish Liber
Hymnorum; &c.
J. H.
van’t
H.
Jacobus Henricus van’t Hoff, LL.D.,
D.Sc., D.M.
See the biographical article van’t
Hoff, Jacobus Henricus.
Isomerism.
J. L.
M.
John Lynton Myres, M.A., F.S.A.,
F.R.G.S.
Wykeham Professor of Ancient
History in the University of
Oxford. Formerly Gladstone
Professor of Greek and Lecturer
in Ancient Geography, University
of Liverpool. Lecturer in Classical
Archaeology in University of
Oxford.
Iberians;
Ionians.
J. Mn. John Macpherson, M.D.
Formerly Inspector-General of
Hospitals, Bengal.
Insanity: Medical
(in part).
J. M.
A. de
L.
Jean Marie Antoine de Lanessan.
See the biographical article,
Lanessan, J. M. A. de.
Indo-China,
French (in
part).
J. M.
M.
John Malcolm Mitchell.
Sometime Scholar of Queen’s
College, Oxford. Lecturer in
Classics, East London College
(University of London). Joint-
Hyacinthus.
59. editor of Grote’s History of
Greece.
J. P.
E.
Jean Paul Hippolyte Emmanuel Adhémar
Esmein.
Professor of Law in the University
of Paris. Officer of the Legion of
Honour. Member of the Institute
of France. Author of Cours
élémentaire d’histoire du droit
français; &c.
Intendant.
J. P.
Pe.
Rev. John Punnett Peters, Ph.D., D.D.
Canon Residentiary, Cathedral of
New York. Formerly Professor of
Hebrew in the University of
Pennsylvania. Director of the
University Expedition to
Babylonia, 1888-1895. Author of
Nippur, or Explorations and
Adventures on the Euphrates.
Irak-Arabi (in
part).
J. S.
Bl.
John Sutherland Black, M.A., LL.D.
Assistant Editor of the 9th edition
of the Encyclopaedia Britannica.
Joint-editor of the Encyclopaedia
Biblica.
Huss, John.
J. S.
Co.
James Sutherland Cotton, M.A.
Editor of the Imperial Gazetteer
of India. Hon. Secretary of the
Egyptian Exploration Fund.
Formerly Fellow and Lecturer of
Queen’s College, Oxford. Author
of India; &c.
India: Geography
and Statistics (in
part); History (in
part);
Indore.
60. J. S.
F.
John Smith Flett, D.Sc., F.G.S.
Petrographer to the Geological
Survey. Formerly Lecturer on
Petrology in Edinburgh
University. Neill Medallist of the
Royal Society of Edinburgh.
Bigsby Medallist of the Geological
Society of London.
Itacolumite.
J. T.
Be.
John Thomas Bealby.
Joint-author of Stanford’s
Europe. Formerly Editor of the
Scottish Geographical Magazine.
Translator of Sven Hedin’s
Through Asia, Central Asia and
Tibet; &c.
Irkutsk (in part).
J. V.* Jules Viard.
Archivist at the National Archives,
Paris. Officer of Public
Instruction. Author of La France
sous Philippe VI. de Valois; &c.
Isabella of
Bavaria.
Jno.
W.
John Westlake, K.C., LL.D.
Professor of International Law,
Cambridge, 1888-1908. One of
the Members for the United
Kingdom of International Court
of Arbitration under the Hague
Convention, 1900-1906. Bencher
of Lincoln’s Inn. Author of A
Treatise on Private International
Law, or the Conflict of Laws:
Chapters on the Principles of
International
Law: Private.
61. International Law, pt. i. “Peace,”
pt. ii. “War.”
L. Count Lützow, Litt.D. (Oxon.), Ph.D.
(Prague), F.R.G.S.
Chamberlain of H.M. the
Emperor of Austria, King of
Bohemia. Hon. Member of the
Royal Society of Literature.
Member of the Bohemian
Academy; &c. Author of
Bohemia, a Historical Sketch;
The Historians of Bohemia
(Ilchester Lecture, Oxford,
1904); The Life and Times of
John Hus; &c.
Hussites.
L. C.
B.
Lewis Campbell Bruce, M.D., F.R.C.P.
Author of Studies in Clinical
Psychiatry.
Insanity: Medical
(in part).
L. Ho. Laurence Housman.
See the biographical article,
Housman, L.
Illustration (in
part).
L. J.
S.
Leonard James Spencer, M.A.
Assistant in Department of
Mineralogy, British Museum.
Formerly Scholar of Sidney
Sussex College, Cambridge, and
Harkness Scholar. Editor of the
Mineralogical Magazine.
Hypersthene;
Ilmenite.
L. T.
D.
Sir Lewis Tonna Dibdin, M.A., D.C.L.,
F.S.A.
Dean of the Arches; Master of
Incense: Ritual
Use.
62. the Faculties; and First Church
Estates Commissioner. Bencher
of Lincoln’s Inn. Author of
Monasticism in England; &c.
M.
Ha.
Marcus Hartog, M.A., D.Sc., F.L.S.
Professor of Zoology, University
College, Cork. Author of
“Protozoa” in Cambridge Natural
History; and papers for various
scientific journals.
Infusoria.
M. Ja. Morris Jastrow, Jun., Ph.D.
Professor of Semitic Languages,
University of Pennsylvania, U.S.A.
Author of Religion of the
Babylonians and Assyrians; &c.
Ishtar.
M. O.
B. C.
Maximilian Otto Bismarck Caspari, M.A.
Reader in Ancient History at
London University. Lecturer in
Greek at Birmingham University,
1905-1908.
Irene (752-803).
N. M. Norman McLean, M.A.
Fellow, Lecturer and Librarian of
Christ’s College, Cambridge.
University Lecturer in Aramaic.
Examiner for the Oriental
Languages Tripos and the
Theological Tripos at Cambridge.
Isaac of Antioch.
O. J.
R. H.
Osbert John Radcliffe Howarth, M.A.
Christ Church, Oxford.
Geographical Scholar, 1901.
Ireland:
Geography.
63. Assistant Secretary of the British
Association.
P. A. Paul Daniel Alphandéry.
Professor of the History of
Dogma, École pratique des
hautes études, Sorbonne, Paris.
Author of Les Idées morales chez
les hétérodoxes latines au début
du XIIIe. siècle.
Inquisition.
P. A.
K.
Prince Peter Alexeivitch Kropotkin.
See the biographical article,
Kropotkin, Prince P. A.
Irkutsk (in part).
P. C.
M.
Peter Chalmers Mitchell, M.A., F.R.S.,
F.Z.S., D.Sc., LL.D.
Secretary to the Zoological
Society of London. University
Demonstrator in Comparative
Anatomy and Assistant to Linacre
Professor at Oxford, 1888-1891.
Examiner in Zoology to the
University of London, 1903.
Author of Outlines of Biology; &c.
Hybridism.
P. Gi. Peter Giles, M.A., LL.D., Litt.D.
Fellow and Classical Lecturer of
Emmanuel College, Cambridge,
and University Reader in
Comparative Philology. Formerly
Secretary of the Cambridge
Philological Society. Author of
Manual of Comparative Philology;
&c.
I;
Indo-European
Languages.
64. P.
Sm.
Preserved Smith, Ph.D.
Rufus B. Kellogg Fellow, Amherst
College, Amherst, Mass.
Innocent I., II.
R. The Right Hon. Lord Rayleigh.
See the biographical article,
Rayleigh, 3rd Baron.
Interference of
Light.
R. A.
S. M.
Robert Alexander Stewart Macalister,
M.A., F.S.A.
St John’s College, Cambridge.
Director of Excavations for the
Palestine Exploration Fund.
Idumaea.
R. Ba. Richard Bagwell, M.A., LL.D.
Commissioner of National
Education for Ireland. Author of
Ireland under the Tudors; Ireland
under the Stuarts.
Ireland: Modern
History.
R. C.
J.
Sir Richard Claverhouse Jebb, D.C.L.,
LL.D.
See the biographical article, Jebb,
Sir Richard Claverhouse.
Isaeus;
Isocrates.
R. G. Richard Garnett. LL.D.
See the biographical article,
Garnett, Richard.
Irving,
Washington.
R. H.
C.
Rev. Robert Henry Charles, M.A., D.D.,
D.Litt.
Grinfield Lecturer, and Lecturer in
Biblical Studies, Oxford. Fellow of
the British Academy. Formerly
Professor of Biblical Greek, Trinity
College, Dublin. Author of Critical
Isaiah, Ascension
of.
65. History of the Doctrine of a
Future Life; Book of Jubilees; &c.
R. L.* Richard Lydekker, F.R.S., F.Z.S., F.G.S.
Member of the Staff of the
Geological Survey of India 1874-
1882. Author of Catalogues of
Fossil Mammals, Reptiles and
Birds in the British Museum; The
Deer of all Lands; &c.
Hyracoidea;
Ibex (in part);
Indri;
Insectivora.
R. P.
S.
R. Phené Spiers, F.S.A., F.R.I.B.A.
Formerly Master of the
Architectural School, Royal
Academy, London. Past President
of Architectural Association.
Associate and Fellow of King’s
College, London. Corresponding
Member of the Institute of
France. Editor of Fergusson’s
History of Architecture. Author of
Architecture; East and West; &c.
Hypaethros.
R. S.
C.
Robert Seymour Conway, M.A., D.Litt.
(Cantab.).
Professor of Latin and Indo-
European Philology in the
University of Manchester.
Formerly Professor of Latin in
University College, Cardiff; and
Fellow of Gonville and Caius
College, Cambridge. Author of
The Italic Dialects.
Iguvium;
Iovilae.
S. The Right Hon. the Earl of Selborne.
See the biographical article,
Hymns.
66. Selborne, 1st Earl of.
R. Tr. Roland Truslove, M.A.
Formerly Scholar of Christ
Church, Oxford. Dean, Fellow
and Lecturer in Classics at
Worcester College, Oxford.
Indo-China,
French (in
part).
S. A.
C.
Stanley Arthur Cook, M.A.
Lecturer in Hebrew and Syriac,
and formerly Fellow, Gonville and
Caius College, Cambridge. Editor
for Palestine Exploration Fund.
Author of Glossary of Aramaic
Inscriptions; The Laws of Moses
and the Code of Hammurabi;
Critical Notes on Old Testament
History; Religion of Ancient
Palestine; &c.
Ishmael.
S. Bl. Sigfus Blöndal.
Librarian of the University of
Copenhagen.
Iceland: Recent
Literature.
T. As. Thomas Ashby, M.A., D.Litt. (Oxon.).
Director of British School of
Archaeology at Rome. Formerly
Scholar of Christ Church, Oxford.
Craven Fellow, 1897. Conington
Prizeman, 1906. Member of the
Imperial German Archaeological
Institute.
Interamna
Lirenas;
Ischia.
T. A.
I.
Thomas Allan Ingram, M.A., LL.D.
Trinity College, Dublin.
Illegitimacy;
67. Insurance (in
part).
T. Ba. Sir Thomas Barclay, M.P.
Member of the Institute of
International Law. Member of the
Supreme Council of the Congo
Free State. Officer of the Legion
of Honour. Author of Problems of
International Practice and
Diplomacy; &c. M.P. for
Blackburn, 1910.
Immunity;
International
Law.
T. F. Rev. Thomas Fowler, M.A., D.D., LL.D.
(1832-1904).
President of Corpus Christi
College, Oxford, 1881-1904.
Honorary Fellow of Lincoln
College. Professor of Logic,
1873-1888. Vice-Chancellor of
the University of Oxford, 1899-
1901. Author of Elements of
Deductive Logic; Elements of
Inductive Logic; Locke (“English
Men of Letters”); Shaftesbury
and Hutcheson (“English
Philosophers”); &c.
Hutcheson,
Francis (in
part).
T. F.
C.
Theodore Freylinghuysen Collier, Ph.D.
Assistant Professor of History,
Williams College, Williamstown,
Mass., U.S.A.
Innocent IX.-
XIII.
T. H.
H.*
Colonel Sir Thomas Hungerford
Holdich, K.C.M.G., K.C.I.E.,
Hon.D.Sc.
Indus.
68. Superintendent, Frontier
Surveys, India, 1892-1898. Gold
Medallist, R.G.S., London, 1887.
Author of The Indian Borderland;
The Countries of the King’s
Award; India; Tibet; &c.
T. K.
C.
Rev. Thomas Kelly Cheyne, D.D.
See the biographical article,
Cheyne, T. K.
Isaiah.
Th. T. Thorvaldur Thoroddsen.
Icelandic Expert and Explorer.
Honorary Professor in the
University of Copenhagen.
Author of History of Icelandic
Geography; Geological Map of
Iceland; &c.
Iceland:
Geography and
Statistics.
W. A.
B. C.
Rev. William Augustus Brevoort
Coolidge, M.A., F.R.G.S., Ph.D.
(Bern).
Fellow of Magdalen College,
Oxford. Professor of English
History, St David’s College,
Lampeter, 1880-1881. Author of
Guide du Haut Dauphiné; The
Range of the Tödi; Guide to
Grindelwald; Guide to
Switzerland; The Alps in Nature
and in History; &c. Editor of The
Alpine Journal, 1880-1881; &c.
Hyères;
Innsbruck;
Interlaken;
Iseo, Lake of;
Isère (River);
Isère
(Department).
W. A.
P.
Walter Alison Phillips, M.A.
Formerly Exhibitioner of Merton
College and Senior Scholar of St
Innocent III., IV.
69. John’s College, Oxford. Author of
Modern Europe; &c.
W. C.
U.
William Cawthorne Unwin, LL.D., F.R.S.,
M.Inst.C.E., M.Inst.M.E.,
A.R.I.B.A.
Emeritus Professor, Central
Technical College, City and Guilds
of London Institute. Author of
Wrought Iron Bridges and Roofs;
Treatise on Hydraulics; &c.
Hydraulics.
W. F.
C.
William Feilden Craies, M.A.
Barrister-at-Law, Inner Temple.
Lecturer on Criminal Law, King’s
College, London. Editor of
Archbold’s Criminal Pleading
(23rd edition).
Indictment.
W. F.
Sh.
William Fleetwood Sheppard, M.A.
Senior Examiner in the Board of
Education, London. Formerly
Fellow of Trinity College,
Cambridge. Senior Wrangler,
1884.
Interpolation.
W. G. William Garnett, M.A., D.C.L.
Educational Adviser to the
London County Council. Formerly
Fellow and Lecturer of St John’s
College, Cambridge. Principal and
Professor of Mathematics,
Durham College of Science,
Newcastle-on-Tyne. Author of
Elementary Dynamics; &c.
Hydrometer.
70. W.
Go.
William Gow, M.A., Ph.D.
Secretary of the British and
Foreign Marine Insurance Co.
Ltd., Liverpool. Lecturer on
Marine Insurance at University
College, Liverpool. Author of
Marine Insurance; &c.
Insurance: Marine.
W. H.
F.
Sir William Henry Flower, F.R.S.
See the biographical article,
Flower, Sir W. H.
Ibex (in part).
W. H.
Po.
W. Haldane Porter.
Barrister-at-Law, Middle Temple.
Ireland: Statistics
and
Administration.
W.
Ma.
Sir William Markby, K.C.I.E.
See the biographical article,
Markby, Sir William.
Indian Law.
W.
McD.
William McDougall, M.A.
Wilde Reader in Mental
Philosophy in the University of
Oxford. Formerly Fellow of St
John’s College, Cambridge.
Hypnotism.
W. M.
L.
Wallace Martin Lindsay, M.A., Litt.D.,
LL.D.
Professor of Humanity, University
of St Andrews. Fellow of the
British Academy. Formerly Fellow
of Jesus College, Oxford. Author
of Handbook of Latin
Inscriptions; The Latin
Language; &c.
Inscriptions; Latin
(in part).
71. W. M.
Ra.
Sir William Mitchell Ramsay, Litt.D.,
D.C.L.
See the biographical article,
Ramsay, Sir W. Mitchell.
Iconium.
W. R.
So.
William Ritchie Sorley, M.A., Litt.D.,
LL.D.
Professor of Moral Philosophy in
the University of Cambridge.
Fellow of King’s College,
Cambridge. Fellow of the British
Academy. Formerly Fellow of
Trinity College. Author of The
Ethics of Naturalism; The
Interpretation of Evolution; &c.
Iamblichus.
W. T.
T.-D.
Sir William Turner Thiselton-Dyer,
F.R.S., K.C.M.G., C.I.E., D.Sc.,
LL.D., Ph.D., F.L.S.
Hon. Student of Christ Church,
Oxford. Director, Royal Botanic
Gardens, Kew, 1885-1905.
Botanical Adviser to Secretary of
State for Colonies, 1902-1906.
Joint-author of Flora of
Middlesex. Editor of Flora
Capenses and Flora of Tropical
Africa.
Huxley.
W.
Wn.
William Watson, D.Sc., F.R.S., A.R.C.S.
Assistant Professor of Physics,
Royal College of Science,
London. Vice-President of the
Physical Society. Author of A Text
Book of Practical Physics; &c.
Inclinometer.
72. W. W.
H.
Sir William Wilson Hunter.
See the biographical article.
Hunter, Sir William Wilson.
India: History (in
part); Geography
and Statistics (in
part).
1 A complete list, showing all individual contributors,
appears in the final volume.
PRINCIPAL UNSIGNED ARTICLES
Husband and
Wife.
Hyacinth.
Hyderabad.
Hydrogen.
Hydropathy.
Hydrophobia.
Ice.
Ice-
Yachting.
Idaho.
Illinois.
Illumination.
Illyria.
Image.
Impeachment.
Income Tax.
Indiana.
Indian
Mutiny.
Indicator.
Infant.
Infanticide.
Infinite.
Influenza.
Inheritance.
Injunction.
Ink.
Inkerman.
International,
The.
Intestacy.
Inverness-
shire.
Investiture.
Iodine.
Iowa.
Ipecacuanha.
Iris.
Iron.
Irrigation.
73. HUSBAND, properly the “head of a household,” but now chiefly
used in the sense of a man legally joined by marriage to a woman,
his “wife”; the legal relations between them are treated below under
Husband and Wife. The word appears in O. Eng. as húsbonda,
answering to the Old Norwegian húsbóndi, and means the owner or
freeholder of a hus, or house. The last part of the word still survives
in “bondage” and “bondman,” and is derived from bua, to dwell,
which, like Lat. colere, means also to till or cultivate, and to have a
household. “Wife,” in O. Eng. wif, appears in all Teutonic languages
except Gothic; cf. Ger. Weib, Dutch wijf, &c., and meant originally
simply a female, “woman” itself being derived from wifman, the
pronunciation of the plural wimmen still preserving the original i.
Many derivations of “wife” have been given; thus it has been
connected with the root of “weave,” with the Gothic waibjan, to fold
or wrap up, referring to the entangling clothes worn by a woman,
and also with the root of vibrare, to tremble. These are all merely
guesses, and the ultimate history of the word is lost. It does not
appear outside Teutonic languages. Parallel to “husband” is
“housewife,” the woman managing a household. The earlier húswif
was pronounced hussif, and this pronunciation survives in the
application of the word to a small case containing scissors, needles
and pins, cottons, &c. From this form also derives “hussy,” now only
used in a depreciatory sense of a light, impertinent girl. Beyond the
meaning of a husband as a married man, the word appears in
connexion with agriculture, in “husbandry” and “husbandman.”
According to some authorities “husbandman” meant originally in the
74. north of England a holder of a “husbandland,” a manorial tenant who
held two ox-gangs or virgates, and ranked next below the yeoman
(see J. C. Atkinson in Notes and Queries, 6th series, vol. xii., and E.
Bateson, History of Northumberland, ii., 1893). From the idea of the
manager of a household, “husband” was in use transferred to the
manager of an estate, and the title was held by certain officials,
especially in the great trading companies. Thus the “husband” of the
East India Company looked after the interests of the company at the
custom-house. The word in this sense is practically obsolete, but it
still appears in “ship’s husband,” an agent of the owners of a ship
who looks to the proper equipping of the vessel, and her repairs,
procures and adjusts freights, keeps the accounts, makes charter-
parties and acts generally as manager of the ship’s employment.
Where such an agent is himself one of the owners of the vessel, the
name of “managing owner” is used. The “ship’s husband” or
“managing owner” must register his name and address at the port of
registry (Merchant Shipping Act 1894, § 59). From the use of
“husband” for a good and thrifty manager of a household, the verb
“to husband” means to economize, to lay up a store, to save.
HUSBAND AND WIFE, Law relating to. For the modes in which
the relation of husband and wife may be constituted and dissolved,
see Marriage and Divorce. The present article will deal only with the
effect of marriage on the legal position of the spouses. The person
chiefly affected is the wife, who probably in all political systems
75. becomes subject, in consequence of marriage, to some kind of
disability. The most favourable system scarcely leaves her as free as
an unmarried woman; and the most unfavourable subjects her
absolutely to the authority of her husband. In modern times the
effect of marriage on property is perhaps the most important of its
consequences, and on this point the laws of different states show
wide diversity of principles.
The history of Roman law exhibits a transition from an extreme
theory to its opposite. The position of the wife in the earliest Roman
household was regulated by the law of Manus. She fell under the
“hand” of her husband,—became one of his family, along with his
sons and daughters, natural or adopted, and his slaves. The
dominion which, so far as the children was concerned, was known as
the patria potestas, was, with reference to the wife, called the
manus. The subject members of the family, whether wife or children,
had, broadly speaking, no rights of their own. If this institution
implied the complete subjection of the wife to the husband, it also
implied a much closer bond of union between them than we find in
the later Roman law. The wife on her husband’s death succeeded,
like the children, to freedom and a share of the inheritance. Manus,
however, was not essential to a legal marriage; its restraints were
irksome and unpopular, and in course of time it ceased to exist,
leaving no equivalent protection of the stability of family life. The
later Roman marriage left the spouses comparatively independent of
each other. The distance between the two modes of marriage may
be estimated by the fact that, while under the former the wife was
one of the husband’s immediate heirs, under the latter she was
called to the inheritance only after his kith and kin had been
exhausted, and only in preference to the treasury. It seems doubtful
how far she had, during the continuance of marriage, a legal right to
76. enforce aliment from her husband, although if he neglected her she
had the unsatisfactory remedy of an easy divorce. The law, in fact,
preferred to leave the parties to arrange their mutual rights and
obligations by private contracts. Hence the importance of the law of
settlements (Dotes). The Dos and the Donatio ante nuptias were
settlements by or on behalf of the husband or wife, during the
continuance of the marriage, and the law seems to have looked with
some jealousy on gifts made by one to the other in any less formal
way, as possibly tainted with undue influence. During the marriage
the husband had the administration of the property.
The manus of the Roman law appears to be only one instance of
an institution common to all primitive societies. On the continent of
Europe after many centuries, during which local usages were
brought under the influence of principles derived from the Roman
law, a theory of marriage became established, the leading feature of
which is the community of goods between husband and wife.
Describing the principle as it prevails in France, Story (Conflict of
Laws, § 130) says: “This community or nuptial partnership (in the
absence of any special contract) generally extends to all the movable
property of the husband and wife, and to the fruits, income and
revenue thereof.... It extends also to all immovable property of the
husband and wife acquired during the marriage, but not to such
immovable property as either possessed at the time of the marriage,
or which came to them afterwards by title of succession or by gift.
The property thus acquired by this nuptial partnership is liable to the
debts of the parties existing at the time of the marriage; to the
debts contracted by the husband during the community, or by the
wife during the community with the consent of the husband; and to
debts contracted for the maintenance of the family.... The husband
alone is entitled to administer the property of the community, and he
77. may alien, sell or mortgage it without the concurrence of the wife.”
But he cannot dispose by will of more than his share of the common
property, nor can he part with it gratuitously inter vivos. The
community is dissolved by death (natural or civil), divorce,
separation of body or separation of property. On separation of body
or of property the wife is entitled to the full control of her movable
property, but cannot alien her immovable property, without her
husband’s consent or legal authority. On the death of either party
the property is divided in equal moieties between the survivor and
the heirs of the deceased.
Law of England.—The English common law as usual followed its
own course in dealing with this subject, and in no department were
its rules more entirely insular and independent. The text writers all
assumed two fundamental principles, which between them
established a system of rights totally unlike that just described.
Husband and wife were said to be one person in the eye of the law
—unica persona, quia caro una et sanguis unus. Hence a man could
not grant or give anything to his wife, because she was himself, and
if there were any compacts between them before marriage they
were dissolved by the union of persons. Hence, too, the old rule of
law, now greatly modified, that husband and wife could not be
allowed to give evidence against each other, in any trial, civil or
criminal. The unity, however, was one-sided only; it was the wife
who was merged in the husband, not the husband in the wife. And
when the theory did not apply, the disabilities of “coverture”
suspended the active exercise of the wife’s legal faculties. The old
technical phraseology described husband and wife as baron and
feme; the rights of the husband were baronial rights. From one point
of view the wife was merged in the husband, from another she was
as one of his vassals. A curious example is the immunity of the wife
78. in certain cases from punishment for crime committed in the
presence and on the presumed coercion of the husband. “So great a
favourite,” says Blackstone, “is the female sex of the laws of
England.”
The application of these principles with reference to the property
of the wife, and her capacity to contract, may now be briefly traced.
The freehold property of the wife became vested in the husband
and herself during the coverture, and he had the management and
the profits. If the wife had been in actual possession at any time
during the marriage of an estate of inheritance, and if there had
been a child of the marriage capable of inheriting, then the husband
became entitled on his wife’s death to hold the estate for his own life
as tenant by the curtesy of England (curialitas).1 Beyond this,
however, the husband’s rights did not extend, and the wife’s heir at
last succeeded to the inheritance. The wife could not part with her
real estate without the concurrence of the husband; and even so she
must be examined apart from her husband, to ascertain whether she
freely and voluntarily consented to the deed.
With regard to personal property, it passed absolutely at common
law to the husband. Specific things in the possession of the wife
(choses in possession) became the property of the husband at once;
things not in possession, but due and recoverable from others
(choses in action), might be recovered by the husband. A chose in
action not reduced into actual possession, when the marriage was
dissolved by death, reverted to the wife if she was the survivor; if
the husband survived he could obtain possession by taking out
letters of administration. A chose in action was to be distinguished
from a specific thing which, although the property of the wife, was
for the time being in the hands of another. In the latter case the
79. Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.
More than just a book-buying platform, we strive to be a bridge
connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.
Join us on a journey of knowledge exploration, passion nurturing, and
personal growth every day!
ebookbell.com