Skip to content

Recipes Lua

Roman Tsisyk edited this page Jul 25, 2016 · 1 revision

Lua Language

Iterate over a Lua table

Remember: ipairs() for arrays-like tables and pairs() for map-like and mixed tables. ipairs() is faster than pairs().

Get the number of items in array-like table

Use operator '#':

Please note this operation has O(log(N)) complexity.

Try not to put nil in arrays. The length operator # will be confused and standard table functions like table.sort() will complain bitterly.

Get the number of items in map-like table

Lua doens't store this value anywhere. Iterate over the table and calculate size:

Swap two variables

Classes and objects

See also:

Run garbage collector

See also collectgarbage documentation.

Tables and nil values

Lua doesn't store nil in tables. a[2] = nil is semantically means delete key 2 from a. However, you can store special NULL value which is semantically identical to nil:

Call a built-in C function

See FFI tutorial for more examples.

Call a C function from a library

See FFI tutorial for more examples.

Metamethods for C objects

Current time with millesecond precision

Call gettimeofday() or clock_gettime() using Foreign Function Interface (FFI):

Developer Guidelines ↗

Architecture

How To ...?

Recipes

Upgrade instructions

Useful links

Old discussions

Personal pages

Clone this wiki locally