SEANK.H.LIAO

lua

notes on lua

lua

comments

variables

builtin, operators

unusual ones

idioms

control flow

do

not really control flow, just a new block

1do
2  -- something
3end
if
1if condition then
2  -- something
3elseif condition then
4  -- something
5else
6  -- something
7end
while, repeat
1-- test before body, repeat until false
2while condition do
3  -- something
4end
5
6-- test after body, repeat until true
7repeat
8  -- something
9until condition
for
1-- numeric, step defaults to 1
2for i = start,finish,step do
3  -- something
4end
5
6-- generic, use with iterators
7for a,b in x do
8  -- something
9end
break

functions

 1-- normal function
 2function function_name (a, b, ...)
 3  -- something
 4  -- access ... with arg[]
 5  return a, b
 6end
 7
 8-- anonymous function, useful in closures
 9function ()
10  return "foo"
11end
12
13-- assign function_name to some_table
14function some_table.function_name ()
15  return "foo"
16end
17
18-- locally scoped function
19local function function_name ()
20  return "foo"
21end

tables

errors

special tables

ref lua 5.4 index