Lua in Relatively Few Words

Lua is a simple, general-purpose scripting language. We chose Lua for Webscript.io because Lua's open-source implementation provides the performance and security that we needed, while being extremely small and efficient. We believe that most people who are new to Lua will pick up the basics in minutes and feel comfortable within hours. What follows is an extremely brief introduction that covers the basics. See http://www.lua.org/docs.html for more thorough Lua reference materials.

Variables and Simple Data Types

Lua has variables and simple types include nil, string, number (integer, floating point), and Boolean values.

Literal values

Typically, Lua strings are enclosed in single or double quotes. They can also be enclosed in double brackets (e.g. [[hello world]]), which allows newlines in the string literal.

Lua Boolean literals are true and false.

Strings

Strings are an essential Lua data type. Common string operations follow:

Assignment

Lua uses '=' as the assignment operator. In addition to typical assignment, Lua also supports multiple assignment:

Operators

Lua uses <, <=, ==. >=, >, ~= as comparison operators on numeric values.

Lua uses and, or, and not as logical operators.

Tables

Lua has only one structured data type, tables, which are associative arrays. The literal constructor "{}" represents a new empty table.

Constructors can also create members.

If the member name is not a proper identifier name, then it must be quoted and wrapped in brackets

If no member names are given, then the constructor is treated like an array, with the key values 1, 2, 3, …. The following are equivalent

Tables can act like records. The following are equivalent:

Functions and Function Calls

Lua has functions and function calls.

Unlike many languages, Lua supports multiple return values.

Lua function calls may omit the parentheses when there is a single argument that is either a string literal or a table constructor. When you see this:

It is syntactic sugar for

Similarly, foo 'bar' is sugar for foo('bar').

Control Statements

Lua has if-elseif-else-then, while-loop, and repeat-loop statements.

Lua also has a general for-loop with numeric indices. The for-loop includes two or three index values that represent the start, end, and (optional) step values.

Lua also includes a for-loop that iterates over the values in a list. This is often used to iterate over the key/value pairs in a table using either pairs(T) or ipairs(T).

Lua includes a break-statement that will exit an enclosing loop:

Advanced

Lua is a rich, modern programming language, and this introduction is only meant to help you get started. Please refer to the excellent online documents to learn the full language. See http://www.lua.org/docs.html for more.