LUA
Lua is a small, fast, embeddable scripting language designed to be:
- Lightweight: very small footprint
- Embeddable: easy to integrated into C/C++ apps
- Flexible: minimal syntax
Install
| ubuntu 24.04 | |
|---|---|
VSCode
Core
Tables are EVERYTHING
Lua doesn't have:
- arrays
- struct
- classes
instead os has tables which as as:
- array
- dictionaries
- objects
- module
Table are dictionary
Nested
Iterate
Missing key return nil
Table as array
Lua starts at 1
Function first class
Function are values:
- store in variables
- pass as argument
- return from functions
Data types
- nil (null)
- number
- string
- boolean
- table
- function
Variables
In Lua, variables are:
- Global by default ❗
- Local only if you explicitly say local
Local variable scope
- block scope
- function scope
- file/module scope
Strings
Multiline strings
Concatenation
Lua uses ..
string methods
| Function | Description | Example | Result |
|---|---|---|---|
string.len(s) |
Get length of string | string.len("abc") |
3 |
#s |
Length operator (shortcut) | #"abc" |
3 |
string.sub(s, i, j) |
Substring from index i to j |
string.sub("hello",1,4) |
"hell" |
string.upper(s) |
Convert to uppercase | string.upper("abc") |
"ABC" |
string.lower(s) |
Convert to lowercase | string.lower("ABC") |
"abc" |
string.reverse(s) |
Reverse string | string.reverse("abc") |
"cba" |
string.find(s, pattern) |
Find pattern (returns indices) | string.find("hello","lo") |
4,5 |
string.match(s, pattern) |
Extract first match | string.match("a1b2","%d+") |
"1" |
string.gmatch(s, pattern) |
Iterate over matches | loop over "a1b2" |
"a1","b2" |
string.gsub(s, pattern, repl) |
Replace occurrences | string.gsub("hi all","all","Lua") |
"hi Lua" |
string.format(fmt, ...) |
Format string (like printf) | string.format("%d",10) |
"10" |
string.byte(s, i) |
Get ASCII/byte value | string.byte("A") |
65 |
string.char(...) |
Convert numbers to chars | string.char(65) |
"A" |
string.rep(s, n) |
Repeat string n times |
string.rep("ab",3) |
"ababab" |