Programming in Lua

(note based on 3rd Edition)

Lua needs no separator between consecutive statements,

Line breaks play no role in Lua’s syntax;

Identifiers (or names) in Lua can be any string of letters, digits, and underscores, not beginning with a digit;

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.

It is not an error to access a non-initialized variable; you just get the value nil as the result:

After this assignment, Lua can eventually reclaim the memory used by the variable.

When the interpreter loads a file, it ignores its first line if this line starts with a hash (#).

The -l option loads a library.

LUA_INIT gives us great power when configuring the stand-alone interpreter,

retrieve its arguments in the predefined global variable arg.

eight basic types in Lua: nil, boolean, number, string, userdata, function, thread, and table.

a global variable has a nil value by default,

you can assign nil to a global variable to delete it.

both the boolean false and nil as false and anything else as true.

You can get the length of a string using the prefix operator # (called the length operator):

We can delimit literal strings also by matching double square brackets,

literal string ends only at the next closing brackets with the same number of equal signs in between

Lua provides automatic conversions between numbers and strings at run time.

When you write it right after a numeral, you must separate them with a space;

tonumber, which returns nil if the string does not denote a proper number:

Tables in Lua are neither values nor variables; they are objects.

When a program has no more references to a table, Lua’s garbage collector will eventually delete the table and reuse its memory.

Lua supports this representation by providing a.name as syntactic sugar for a["name"].

However, it is customary in Lua to start arrays with one

a list without holes a sequence.

Lua can call functions written in Lua and functions written in C.

The == operator tests for equality; the ~= operator is the negation of equality.

If the values have different types, Lua considers them not equal.

nil is equal only to itself.

Values other than numbers and strings can be compared only for equality (and inequality).

The logical operators are and, or, and not. Like control structures,

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:

Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.

The not operator always returns a boolean value:

Lua denotes the string concatenation operator by .. (two dots). If any operand is a number, Lua converts this number to a string.

The length operator works on strings and tables.

a table with no numeric keys is a sequence with length zero.

If you really need to handle lists with holes, you should store the length explicitly somewhere.

a = {x=10, y=20}

explicitly write the index to be initialized as an expression,

{x = 0, y = 0} is equivalent to {["x"] = 0, ["y"] = 0},

You can always put a comma after the last entry.

you can always use a semicolon instead of a comma in a constructor.

Lua allows multiple assignment,

In a multiple assignment, Lua first evaluates all values and only then executes the assignments.

To initialize a set of variables, you must provide a value for each one:

We create local variables with the local statement:

These do blocks are useful also when you need finer control over the scope of some local variables:

it also speeds up the access to foo.

with if for conditional execution and while, repeat, and for for iteration.

end terminates if, for and while structures; until terminates repeat structures.

Because Lua has no switch statement, such chains are somewhat common.

This statement does the test after the body, so that it always executes the body at least once.

The for statement has two variants: the numeric for and the generic for.

If you want a loop without an upper limit, you can use the constant math.huge:

If you want to end a for loop before its normal termination, use break

pairs, a handy iterator function to traverse a table

break statement to finish a loop. This statement breaks the inner loop

return is the last statement of the then block.

goto followed by the label name, which can be any valid identifier.

like in ::name::.

jump out of a function.

you cannot jump out of a function.

A continue statement is simply a goto to a label at the end of a loop block; a redo statement jumps to the beginning of the block:

labels are considered void statements.

Search Blog: