Lua Notes

Posted by Will on July 18, 2022

Fundamentals

1
2
3
4
5
6
print("Hello World!")
-- this is a one-line comment
--[[
this is a multi-line comment
it can go here
]]

Math

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
print(math.floor(2.345)) -- 2
print(math.ceil(2.345))  -- 3
print(math.max(2,3))     -- 3
print(math.min(2,3))     -- 2
print(math.pow(8,2))     -- 64
print(math.sqrt(64))     -- 8

print(math.random())     -- 0 ~ 1
print(math.random(10))   -- 1 ~ 10
print(math.random(3, 8)) -- 3 ~ 8

math.randomseed(os.time())

str = io.read()
num = tonumber(str)

String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
name = "nerdzzh"

print(#name) -- 7
print(string.find(name, "zzh")) -- 4
print(string.gsub(name, "nerd", "nurd")) -- "nurdzzh"
print(string.upper(name)) -- "NERDZZH"
print(string.lower(name)) -- "nerdzzh"

longstr = [[
this is a long string
it can go here
]]

print(name .. " is a nerd") -- nerdzzh is a nerd

Conditionals

1
2
3
4
5
6
7
8
9
10
11
age = 13

if age < 16 then
  print("study")
elseif age < 18 then
  print("drive")
else
  print("work")
end -- "study"

print(not(false or true and false)) -- true

Tables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
john = {
  name = "john smith",
  age = 30,
  job = "specialized agent",
  hobbies = {"surfing", "driving", "karate"}
}

print(john["name"]) -- "john smith"
print(john.name) -- "john smith"
print(john.hobbies[1]) -- "surfing"

nat = {1, 2, 3, 4, 5}
table.insert(nat, 1, 0) -- insert 0 at index 1
print(table.concat(nat, ", ")) -- 0, 1, 2, 3, 4, 5
table.remove(nat, 1) -- remove at index 1
print(table.concat(nat, ", ")) -- 1, 2, 3, 4, 5
table.sort(nat, function(a, b) return a > b end) -- sort in reverse
print(table.concat(nat, ", ")) -- 5, 4, 3, 2, 1

Looping

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- 1. while loop
i = 1
while i < 10 do
  print(i)
  i = i + 1

  if i == 8 then
    break
  end
end -- 1 2 3 4 5 6 7

-- 2. repeat ... until loop
repeat
  print("Enter your guess:")
  guess = io.read()
until tonumber(guess) == 15

-- 3. for loop
for i = 1, 10, 2 do
  print(i)
end -- 1 3 5 7 9

Functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function split_string(str)
  tab = {}
  local i = 1
  for s in string.gmatch(str, "[^%s]+") do
    tab[i] = s
    i = i + 1
  end
  return tab
end

stab, snum = split_string("nerdzzh is a nerd")
for k, v in pairs(stab) do
  print(v)
end -- nerdzzh\nis\na\nnerd

-- unknown number of parameters
function sumall(...)
  local ans = 0
  for k, v in pairs {...} do
    ans = ans + v
  end
  return ans
end

print(sum(1, 2, 3, 4)) -- 10

-- anonymous function
sumfunc = function(x, y)
  return x + y
end

print(sumfunc(1, 2)) -- 3

-- return a function
multfunc = function(factor)
  return function(x)
    return x * factor
  end
end

twice = multfunc(2)
thrice = multfunc(3)

print(twice(5)) -- 10
print(thrice(5)) -- 15

Modules

1
2
3
4
5
6
7
8
9
10
11
12
-- file "hello.lua"
local M = {}

function M.say_hello()
  print("hello")
end

M.self_intro = function(name)
  print("name: ", name)
end

return M
1
2
3
4
5
6
-- file "test.lua"
require "hello".say_hello() -- "hello"

func = require "hello".self_intro

func("nerdzzh") -- "name: nerdzzh"