Lua - Tutorialspoint

 Lua

About the Tutorial

Lua is an open source language built on top of C programming language. Lua has its value across multiple platforms ranging from large server systems to small mobile applications. This tutorial covers various topics ranging from the basics of Lua to its scope in various applications.

Audience

This tutorial is designed for all those readers who are looking for a starting point to learn Lua. It has topics suitable for both beginners as well as advanced users.

Prerequisites

It is a self-contained tutorial and you should be able to grasp the concepts easily even if you are a total beginner. However it would help if you have a basic understanding of working with a simple text editor and command line.

Copyright & Disclaimer

Copyright 2015 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at

contact@

i

Lua

Table of Contents

About the Tutorial .....................................................................................................................................i Audience .................................................................................................................................................... i Prerequisites .............................................................................................................................................. i Copyright & Disclaimer ..............................................................................................................................i Table of Contents......................................................................................................................................ii

1. OVERVIEW ............................................................................................................................1

Features ....................................................................................................................................................1 How Lua is Implemented?.........................................................................................................................1 Learning Lua..............................................................................................................................................2 Some Uses of Lua ......................................................................................................................................2

2. ENVIRONMENT .....................................................................................................................3

Try it Option Online ..................................................................................................................................3 Local Environment Setup ..........................................................................................................................3 Text Editor ................................................................................................................................................3 The Lua Interpreter ...................................................................................................................................4 The Lua Compiler ......................................................................................................................................4 Installation on Windows ...........................................................................................................................4 Installation on Linux..................................................................................................................................4 Installation on Mac OS X ...........................................................................................................................5 Lua IDE......................................................................................................................................................5

3. BASIC SYNTAX .......................................................................................................................7

First Lua Program ......................................................................................................................................7 Tokens in Lua ............................................................................................................................................8 Comments ................................................................................................................................................9

ii

Lua

Identifiers .................................................................................................................................................9 Keywords ..................................................................................................................................................9 Whitespace in Lua...................................................................................................................................10

4. VARIABLES .......................................................................................................................... 11

Variable Definition in Lua........................................................................................................................11 Variable Declaration in Lua .....................................................................................................................12 Lvalues and Rvalues in Lua......................................................................................................................13

5. DATA TYPES ........................................................................................................................ 14

Type Function .........................................................................................................................................14

6. OPERATORS ........................................................................................................................ 16

Arithmetic Operators ..............................................................................................................................16 Relational Operators...............................................................................................................................17 Logical Operators ....................................................................................................................................20 Misc Operators .......................................................................................................................................22 Operators Precedence in Lua ..................................................................................................................22

7. LOOPS.................................................................................................................................25

while loop ...............................................................................................................................................26 for loop ...................................................................................................................................................27 repeat...until loop ...................................................................................................................................29 nested loops ...........................................................................................................................................31 Loop Control Statement..........................................................................................................................32 break statement .....................................................................................................................................33 The Infinite Loop.....................................................................................................................................34

8. DECISION MAKING ..............................................................................................................35

if statement ............................................................................................................................................36

iii

Lua

if...else statement ...................................................................................................................................37 The if...else if...else Statement................................................................................................................39 nested if statements ...............................................................................................................................40

9. FUNCTIONS ......................................................................................................................... 42

Defining a Function .................................................................................................................................42 Function Arguments................................................................................................................................43 Calling a Function....................................................................................................................................43 Assigning and Passing Functions .............................................................................................................44 Function with Variable Argument ...........................................................................................................45

10. STRINGS .............................................................................................................................. 46

String Manipulation ................................................................................................................................47 Case Manipulation ..................................................................................................................................48 Replacing a Substring ..............................................................................................................................48 Finding and Reversing .............................................................................................................................49 Formatting Strings ..................................................................................................................................49 Character and Byte Representations.......................................................................................................50 Other Common Functions .......................................................................................................................51

11. ARRAYS ............................................................................................................................... 52

One-Dimensional Array...........................................................................................................................52 Multi-Dimensional Array.........................................................................................................................53

12. ITERATORS .......................................................................................................................... 56

Generic For Iterator ................................................................................................................................56 Stateless Iterators ...................................................................................................................................56 Stateful Iterators.....................................................................................................................................58

iv

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download