lua

notes on lua

SEAN K.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

do
  -- something
end
if
if condition then
  -- something
elseif condition then
  -- something
else
  -- something
end
while, repeat
-- test before body, repeat until false
while condition do
  -- something
end

-- test after body, repeat until true
repeat
  -- something
until condition
for
-- numeric, step defaults to 1
for i = start,finish,step do
  -- something
end

-- generic, use with iterators
for a,b in x do
  -- something
end
break

functions

-- normal function
function function_name (a, b, ...)
  -- something
  -- access ... with arg[]
  return a, b
end

-- anonymous function, useful in closures
function ()
  return "foo"
end

-- assign function_name to some_table
function some_table.function_name ()
  return "foo"
end

-- locally scoped function
local function function_name ()
  return "foo"
end

tables

errors

special tables

ref lua 5.4 index