Programming in Lua, Fourth Edition Roberto Ierusalimschy Programming in Lua, Fourth Edition Roberto Ierusalimschy Copyright © 2016, 2003 Roberto Ierusalimschy
Total Page:16
File Type:pdf, Size:1020Kb
Last update: Sat Mar 18 22:00:08 GMT 2017 (build 21) Programming in Lua, Fourth Edition Roberto Ierusalimschy Programming in Lua, Fourth Edition Roberto Ierusalimschy Copyright © 2016, 2003 Roberto Ierusalimschy Feisty Duck Digital Book Distribution www.feistyduck.com Licensed for the exclusive use of: Eric Taylor <[email protected]> Table of Contents About the Book ................................................................................................................. ix I. The Basics ...................................................................................................................... 1 1. Getting Started ........................................................................................................ 4 Chunks .............................................................................................................. 4 Some Lexical Conventions ................................................................................... 6 Global Variables ................................................................................................. 7 Types and Values ................................................................................................ 7 Nil ............................................................................................................ 8 Booleans .................................................................................................... 8 The Stand-Alone Interpreter .................................................................................. 9 2. Interlude: The Eight-Queen Puzzle ............................................................................ 12 3. Numbers ............................................................................................................... 15 Numerals .......................................................................................................... 15 Arithmetic Operators .......................................................................................... 16 Relational Operators .......................................................................................... 17 The Mathematical Library ................................................................................... 18 Random-number generator .......................................................................... 18 Rounding functions .................................................................................... 18 Representation Limits ......................................................................................... 19 Conversions ...................................................................................................... 21 Precedence ....................................................................................................... 22 Lua Before Integers ........................................................................................... 22 4. Strings ................................................................................................................. 24 Literal strings .................................................................................................... 24 Long strings ..................................................................................................... 25 Coercions ......................................................................................................... 26 The String Library ............................................................................................. 27 Unicode ........................................................................................................... 29 5. Tables .................................................................................................................. 33 Table Indices .................................................................................................... 33 Table Constructors ............................................................................................. 35 Arrays, Lists, and Sequences ............................................................................... 36 Table Traversal ................................................................................................. 38 Safe Navigation ................................................................................................. 38 The Table Library ............................................................................................. 39 6. Functions .............................................................................................................. 42 Multiple Results ................................................................................................ 43 Variadic Functions ............................................................................................. 45 The function table.unpack ............................................................................ 47 Proper Tail Calls ............................................................................................... 48 7. The External World ................................................................................................ 50 The Simple I/O Model ....................................................................................... 50 The Complete I/O Model .................................................................................... 53 Other Operations on Files ................................................................................... 54 Other System Calls ............................................................................................ 55 Running system commands ......................................................................... 55 8. Filling some Gaps .................................................................................................. 57 Local Variables and Blocks ................................................................................. 57 Control Structures .............................................................................................. 58 if then else ............................................................................................... 58 while ....................................................................................................... 59 iii Programming in Lua, Fourth Edition repeat ..................................................................................................... 59 Numerical for ........................................................................................... 60 Generic for .............................................................................................. 60 break, return, and goto ..................................................................................... 61 II. Real Programming ......................................................................................................... 65 9. Closures ............................................................................................................... 68 Functions as First-Class Values ............................................................................ 68 Non-Global Functions ........................................................................................ 69 Lexical Scoping ................................................................................................ 71 A Taste of Functional Programming ..................................................................... 74 10. Pattern Matching .................................................................................................. 77 The Pattern-Matching Functions ........................................................................... 77 The function string.find ...................................................................... 77 The function string.match .................................................................... 77 The function string.gsub ...................................................................... 78 The function string.gmatch .................................................................. 78 Patterns ............................................................................................................ 78 Captures ........................................................................................................... 82 Replacements .................................................................................................... 83 URL encoding .......................................................................................... 84 Tab expansion ........................................................................................... 86 Tricks of the Trade ............................................................................................ 86 11. Interlude: Most Frequent Words ............................................................................. 90 12. Date and Time ..................................................................................................... 92 The Function os.time ..................................................................................... 92 The Function os.date ..................................................................................... 93 Date–Time Manipulation .................................................................................... 95 13. Bits and Bytes ..................................................................................................... 97 Bitwise Operators .............................................................................................