Damien Gonot
Home Blog Notes About

Lua

Homepage / Notes / Computer Science / Programming Languages / Lua

Lightweight, embeddable scripting language

Language Features

Basics

return "Hello, Lua!"
Hello, Lua!
print("Hello, Lua!")
Hello, Lua!

If-Else

if true then
  return 'this is true'
else
  return 'this is false'
end
this is true

Functions

function double(x)
  return x * 2
end

return double(2)
4

Tables

t = {key1 = 'value1', key2 = 'value2'}
return t.key1
value1

"List" (actually a table with consecutive integer keys) Return the length of the list:

v = {'value1', 'value2', 'value3'}
return #v
3

Indices start at 1

v = {'value1', 'value2', 'value3'}
return v[2]
value2