2025-09-01 11:37:45 +01:00
---
title: Lua
date: 2025-09-01 10:07:20
background: bg-[##00007f ]
tags:
- script
- interpret
categories:
- Programming
intro: |
The [Lua ](https://www.lua.org/ ) cheat sheet is a one-page reference sheet for the Lua 5.4 programming language.
plugins:
- copyCode
- runCode
---
## Getting Started
### Introduction
- [Lua ](https://www.lua.org/ ) _ (lua.org) _
- [Lua Document ](https://www.lua.org/docs.html ) _ (lua.org/docs.html) _
- [Learn X in Y minutes ](https://learnxinyminutes.com/docs/lua/ ) _ (learnxinyminutes.com) _
### Hello World
``` lua
print ( " Hello, World! " ) -- Hello, World!
-- You can omit parentheses if the argument is one string or table literal
print " Hello, World! " -- Hello, World!
print [[multi-line
Hello
World]]
```
The famous "Hello World" program in Lua
### Variables
``` lua
local age = 18 -- local variable
boys , girls = 2 , 3 -- global variables
2025-09-01 13:02:04 +01:00
-- Now x = 1, y = 2, z = 3, and 4 is thrown away.
x , y , z = 1 , 2 , 3 , 4
2025-09-01 12:51:52 +01:00
-- Undefined variables return nil.
-- This is not an error:
foo = anUnknownVariable -- Now foo = nil.
2025-09-01 11:37:45 +01:00
-- Variables are global by default unless declared with local.
```
Lua is a dynamically typed language and hence only the values will have types not the variables.
### Data Types {.col-span-2 .row-span-2}
| Value Type | Description |
| ---------- | ---------------------------------------------- |
| `number` | Represent (double-precision) numbers |
| `string` | Represents text |
| `nil` | Differentiates between values with data or not |
| `boolean` | true or false value |
| `function` | Represents a sub routine |
| `userdata` | Represents arbitrary C data |
| `thread` | Represents independent threads of execution |
| `table` | key-value pair, or array. |
2025-09-01 14:29:20 +01:00
See: [Data Type APIs ](#data-type-apis )
2025-09-01 11:59:47 +01:00
2025-09-01 12:32:09 +01:00
### IO
``` lua
-- Writes data to the standard output or a file.
io.write ( " Enter your name: " )
-- Reads input from the user or a file. You can specify formats like "*l" (line), "*n" (number), or "*a" (all).
name = io.read ( )
io.write ( " Enter your age: " )
age = io.read ( )
```
### Comments
``` lua
-- This is a single line comments
```
``` lua
--[[
Comments (multi-line) could also be written like this
--]]
```
2025-09-01 11:37:45 +01:00
## Operators
### Arithmetic
``` lua
-- add
result = 10 + 30 -- => 40
-- subtract
result = 40 - 10 -- => 30
-- multiply
result = 50 * 5 -- => 250
-- divide (float division)
result = 16 / 4 -- => 4.0
-- divide (integer division, Lua 5.3+)
result = 16 // 4 -- => 4
-- modulo
result = 25 % 2 -- => 1
-- power
result = 5 ^ 3 -- => 125
-- unary minus
a = 12
result = - a -- => -12
```
### Relational
``` lua
a = 10
b = 20
-- equals
print ( a == b ) -- false
-- not equals
print ( a ~= b ) -- true
-- greater than
print ( a > b ) -- false
-- less than
print ( a < b ) -- true
-- greater than or equals
print ( a >= b ) -- false
-- less than or equals
print ( a <= b ) -- true
```
2025-09-01 11:47:40 +01:00
### Logical
``` lua
-- and
false and nil --> false
0 and 20 --> 20
10 and 20 --> 20
-- or
true or false --> true
10 or 0 --> 10
12 or 4 --> 12
-- not
not true --> false
```
2025-09-01 11:59:47 +01:00
2025-09-01 12:51:52 +01:00
Only nil and false are falsy; 0 and '' are true!
2025-09-01 12:15:55 +01:00
## Conditionals
### if-else {.col-span-3 .row-span-2}
``` lua
num = 15
if num > 10 then
print ( " num is greater than 10 " )
elseif num < 10 then
print ( " num is smaller than 10 " )
else
print ( " num is 10 " )
end
2025-09-01 12:54:48 +01:00
-- making a ternary operator
-- This is similar to the a?b:c operator in C/js:
ans = aBoolValue and ' yes ' or ' no ' --> 'no'
2025-09-01 12:15:55 +01:00
```
## Loops
### While loop
``` lua
i = 1
while i <= 5 do
print ( i )
i = i + 1
end
```
### For loop
``` lua
-- Numeric for loop (start, end, step)
for i = 1 , 5 , 1 do
print ( i )
end
-- Generic for loop for tables
t = { 10 , 20 , 30 }
for k , v in ipairs ( t ) do
print ( k , v ) -- prints 1 10, 2 20, 3 30
end
```
### Repeat-until loop
``` lua
i = 1
repeat
print ( i )
i = i + 1
until i > 5
```
### Breaking out
``` lua
while x do
2025-09-01 12:52:52 +01:00
if condition then
break
end
2025-09-01 12:15:55 +01:00
end
```
2025-09-01 12:37:29 +01:00
### Continue
``` lua
-- prints even numbers in [|1,10|]
for i = 1 , 10 do
if i % 2 == 1 then
goto continue
end
print ( i )
:: continue ::
end
```
2025-09-01 12:51:52 +01:00
## Functions
2025-09-01 12:58:58 +01:00
### Creating fns
2025-09-01 12:51:52 +01:00
``` lua
function myFunction ( )
return 1
end
function myFunctionWithArgs ( a , b )
-- ...
end
-- function as arg
function operate ( a , b , func )
return func ( a , b )
end
-- anonymous function
function ( parameters )
-- body of the function
end
local greet = function ( name )
return " Hello, " .. name
end
-- Not exported in the module
local function myPrivateFunction ( )
end
-- Splats
function doAction ( action , ... )
print ( " Doing ' " .. action .. " ' to " , ... )
end
```
2025-09-01 12:58:58 +01:00
### Invoking fns {.col-span-2}
2025-09-01 12:51:52 +01:00
``` lua
myFunction ( )
print ( greet ( " Lua " ) ) -- Output: Hello, Lua
-- function as arg
local result = operate ( 5 , 3 , function ( x , y )
return x + y
end )
print ( result ) -- Output: 8
doAction ( ' write ' , " Shirley " , " Abed " )
--> Doing 'write' to Shirley Abed
```
2025-09-01 12:59:35 +01:00
You can omit parentheses if the argument is one string or table literal
``` lua
print " Hello World " --> print("Hello World")
dofile ' a.lua ' --> dofile ('a.lua')
print [[a multi-line --> print([[a multi-line
message]] message ] ] )
f { x = 10 , y = 20 } --> f({x=10, y=20})
type { } --> type({})
```
2025-09-01 11:59:47 +01:00
## Data Type APIs
2025-09-01 14:25:53 +01:00
### Global functions {.col-span-2 .row-span-2}
2025-09-01 14:06:28 +01:00
Assert
``` lua
local my_table = { }
assert ( my_table , " my_table should exist! " ) -- This will not fail
local a = nil
-- This will cause a runtime error with the message "a is nil"
assert ( a , " a is nil " )
```
Type
``` lua
local my_var = 10
print ( type ( my_var ) ) -- "number"
local my_str = " hello "
print ( type ( my_str ) ) -- "string"
local my_func = function ( ) end
print ( type ( my_func ) ) -- "function"
```
Dofile & Loadfile
``` lua
-- Using dofile
dofile ( " my_file.lua " ) -- Executes my_file.lua immediately
-- Using loadfile
local my_func = loadfile ( " my_file.lua " )
if my_func then
-- my_file.lua is valid, now execute it
my_func ( )
else
print ( " Error loading file. " )
end
```
Pairs
``` lua
local my_table = { 10 , " hello " , 20 , name = " Lua " }
print ( " Using pairs: " )
for key , value in pairs ( my_table ) do
print ( key , value )
end
print ( " Using ipairs: " )
for key , value in ipairs ( my_table ) do
print ( key , value )
end
```
To number
``` lua
local num1 = tonumber ( " 34 " )
print ( num1 , type ( num1 ) ) -- 34 number
local num2 = tonumber ( " 34.5 " )
print ( num2 , type ( num2 ) ) -- 34.5 number
local num3 = tonumber ( " abc " )
print ( num3 ) -- nil (conversion failed)
local hex_num = tonumber ( " 8f " , 16 )
print ( hex_num ) -- 143 (8 * 16 + 15)
```
2025-09-01 11:59:47 +01:00
### Strings
``` lua
s = " Hello "
2025-09-01 14:06:28 +01:00
```
Concatenation
2025-09-01 11:59:47 +01:00
2025-09-01 14:06:28 +01:00
``` lua
2025-09-01 11:59:47 +01:00
s .. " there " -- => Hello there
2025-09-01 14:06:28 +01:00
```
2025-09-01 11:59:47 +01:00
2025-09-01 14:06:28 +01:00
Commonly used methods
``` lua
2025-09-01 11:59:47 +01:00
s : upper ( ) -- => HELLO
s : lower ( ) -- => hello
s : len ( ) -- => 5
s : find ( " o " ) -- => 5
s : reverse ( ) -- => olleH
2025-09-01 14:06:28 +01:00
```
2025-09-01 11:59:47 +01:00
2025-09-01 14:06:28 +01:00
Sub
2025-09-01 11:59:47 +01:00
2025-09-01 14:06:28 +01:00
``` lua
local s = " programming "
s : sub ( 3 , 7 ) -- (extracts substring) => "ogram"
```
2025-09-01 11:59:47 +01:00
2025-09-01 14:06:28 +01:00
Gsub
2025-09-01 11:59:47 +01:00
2025-09-01 14:06:28 +01:00
``` lua
s : gsub ( ) --> (substitutes all matches)
2025-09-01 11:59:47 +01:00
```
2025-09-01 13:09:11 +01:00
2025-09-01 14:06:28 +01:00
Char
2025-09-01 13:09:11 +01:00
2025-09-01 14:06:28 +01:00
``` lua
s = " ha "
s : rep ( 3 ) -- // repeats 3 times -> "hahaha"
local s_char = string.char ( 72 , 101 , 108 , 108 , 111 )
print ( s_char ) -- "Hello"
```
2025-09-01 13:09:11 +01:00
2025-09-01 14:06:28 +01:00
Format
2025-09-01 13:09:11 +01:00
``` lua
2025-09-01 14:06:28 +01:00
local name = " Alice "
local age = 30
local formatted = string.format ( " My name is %s and I am %d years old. " , name , age )
print ( formatted ) -- "My name is Alice and I am 30 years old."
```
2025-09-01 13:09:11 +01:00
2025-09-01 14:06:28 +01:00
Others
``` lua
s : match ( )
s : gmatch ( )
s : dump ( )
s : byte ( )
2025-09-01 13:09:11 +01:00
```
2025-09-01 14:25:53 +01:00
### placeholder
2025-09-01 14:06:28 +01:00
2025-09-01 14:25:53 +01:00
### Math {.col-span-2 .row-span-2}
2025-09-01 14:22:39 +01:00
``` lua
math.abs ( - 5 ) -- => 5
-- Returns the absolute value of x.
```
---
``` lua
math.acos ( 1 ) -- => 0
-- Returns the arc cosine of x in radians.
```
---
``` lua
math.asin ( 0 ) -- => 0
-- Returns the arc sine of x in radians.
```
---
``` lua
math.atan ( y , x ) ` -- => arctangent of y/x
-- Returns the arctangent of `y/x` in radians, using the signs of both arguments to determine the correct quadrant.
```
---
``` lua
math.ceil ( x ) ` -- => smallest integer greater than or equal to x
-- Returns the smallest integer value not less than `x`.
```
---
``` lua
math.cos ( x ) ` -- => cosine of x
-- Returns the cosine of `x` (assumed to be in radians).
```
---
``` lua
math.deg ( x ) ` -- => radians to degrees
-- Returns the angle `x` (in radians) converted to degrees.
```
---
``` lua
math.exp ( x ) ` -- => e^x
-- Returns the value of $e^x$, where $e$ is the base of natural logarithms.
```
---
``` lua
math.floor ( x ) ` -- => largest integer less than or equal to x
-- Returns the largest integer value not greater than `x`.
```
---
``` lua
math.fmod ( x , y ) ` -- => remainder of x divided by y
-- Returns the remainder of `x` divided by `y`, with the same sign as `x`.
```
---
``` lua
math.log ( x , base ) ` -- => logarithm of x
-- Returns the logarithm of `x` in the given `base`. If `base` is not provided, it defaults to the natural logarithm.
```
---
``` lua
math.max ( x , ... ) ` -- => largest argument
-- Returns the largest among its arguments.
```
---
``` lua
math.min ( x , ... ) ` -- => smallest argument
-- Returns the smallest among its arguments.
```
---
``` lua
math.modf ( x ) ` -- => integer and fractional parts
-- Returns two numbers: the integer part of `x` and the fractional part.
```
---
``` lua
math.pow ( x , y ) ` -- => x^y
-- Returns `x` raised to the power of `y`.
```
---
``` lua
math.rad ( x ) ` -- => degrees to radians
-- Returns the angle `x` (in degrees) converted to radians.
```
---
``` lua
math.random ( m , n ) ` -- => random number
-- Returns a pseudo-random number. When called without arguments, returns a float in the range $[0, 1)$. With one argument `n`, returns an integer in the range $[1, n]$. With two arguments `m` and `n`, returns an integer in the range $[m, n]$.
```
---
``` lua
math.sin ( x ) ` -- => sine of x
-- Returns the sine of `x` (assumed to be in radians).
```
---
``` lua
math.sqrt ( x ) ` -- => square root of x
-- Returns the non-negative square root of `x`.
```
---
2025-09-01 14:06:28 +01:00
``` lua
2025-09-01 14:22:39 +01:00
math.tan ( x ) ` -- => tangent of x
-- Returns the tangent of `x` (assumed to be in radians).
2025-09-01 13:09:11 +01:00
```
2025-09-01 13:16:52 +01:00
2025-09-01 14:25:53 +01:00
### Table basics
``` lua
-- Array-like table (one-indexed)
local colors = { " red " , " green " , " blue " }
print ( colors [ 1 ] ) -- "red"
-- Dictionary-like table
local user = { name = " Jane " , age = 25 }
print ( user.name ) -- "Jane"
print ( user [ " age " ] ) -- 25
-- Mixed table
local mixed = { 1 , " two " , key = " value " }
print ( mixed [ 1 ] ) -- 1
print ( mixed.key ) -- "value"
-- Getting the length of an array-like table
print ( # colors ) -- 3
```
### Tables
``` lua
local my_table = { 10 , 20 }
-- insert (appends 30 to the end)
table.insert ( my_table , 30 )
-- insertion (inserts 2 in position 1)
table.insert ( my_table , 1 , 2 )
-- remove (remove item in position 3)
table.remove ( my_table , 3 )
-- Default numerical sort
local numbers = { 5 , 2 , 8 , 1 }
table.sort ( numbers ) -- {1, 2, 5, 8}
-- Custom sort for descending order
local numbers_desc = { 5 , 2 , 8 , 1 }
table.sort ( numbers_desc , function ( a , b )
return a > b
end ) -- {8, 5, 2, 1}
-- concat
local fruit = { " apple " , " banana " , " cherry " }
local fruit_string = table.concat ( fruit , " , " )
print ( fruit_string ) -- apple, banana, cherry
```
2025-09-01 14:06:28 +01:00
## Misc
2025-09-01 13:16:52 +01:00
### Classes {.col-span-2 .row-span-2}
Classes aren't built in; there are different ways to make them using tables and metatables.
``` lua
```
Short explanation; what we are trying to do is basically making a table that can hold data and functions
``` lua
Dog = { }
function Dog : new ( )
newObj = { sound = ' woof ' }
self.__index = self
return setmetatable ( newObj , self )
end
function Dog : makeSound ( )
print ( ' I say ' .. self.sound )
end
mrDog = Dog : new ( )
mrDog : makeSound ( ) -- 'I say woof'
```
Inheritance
``` lua
LoudDog = Dog : new ( )
function LoudDog : makeSound ( )
s = self.sound .. ' '
print ( s .. s .. s )
end
seymour = LoudDog : new ( )
seymour : makeSound ( ) -- 'woof woof woof'
```
Another example
``` lua
Account = { }
function Account : new ( balance )
local t = setmetatable ( { } , { __index = Account } )
-- Your constructor stuff
t.balance = ( balance or 0 )
return t
end
function Account : withdraw ( amount )
print ( " Withdrawing " .. amount .. " ... " )
self.balance = self.balance - amount
self : report ( )
end
function Account : report ( )
print ( " Your current balance is: " .. self.balance )
end
a = Account : new ( 9000 )
a : withdraw ( 200 ) -- method call
```
2025-09-01 14:06:28 +01:00
### Meta-tables
A metatable is simply a table with functions in it.
``` lua
mt = { }
mt.__tostring = function ( ) return " lol " end
mt.__add = function ( b ) ... end -- a + b
mt.__mul = function ( b ) ... end -- a * b
mt.__index = function ( k ) ... end -- Lookups (a[k] or a.k)
mt.__newindex = function ( k , v ) ... end -- Setters (a[k] = v)
```
Metatables allow you to override behavior of another table.
``` lua
mytable = { }
setmetatable ( mytable , mt )
print ( myobject )
```
### Files
``` lua
local file = io.open ( " test.txt " , " w " )
if file then
file : write ( " Hello from Lua! " )
io.close ( file )
end
local file = io.open ( " test.txt " , " r " )
if file then
local content = file : read ( " *a " ) -- read all content
print ( content )
io.close ( file )
end
```