logo
💻 编程语言

Lua

Lua Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 编程语言🧭 Markdown 速查🏷️ 2 个标签
#lua#scripting
向下滚动查看内容
返回全部 Cheat Sheets

Getting Started

Introduction
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

-- Now x = 1, y = 2, z = 3, and 4 is thrown away.
x, y, z = 1, 2, 3, 4

-- Undefined variables return nil.
-- This is not an error:
foo = anUnknownVariable  -- Now foo = nil.

-- 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
Value TypeDescription
numberRepresent (double-precision) numbers
stringRepresents text
nilDifferentiates between values with data or not
booleantrue or false value
functionRepresents a sub routine
userdataRepresents arbitrary C data
threadRepresents independent threads of execution
tablekey-value pair, or array.

See: Data Type APIs

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
--]]

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
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

Only nil and false are falsy; 0 and '' are true!

Conditionals

if-else
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

-- making a ternary operator
-- This is similar to the a?b:c operator in C/js:
ans = aBoolValue and 'yes' or 'no'  --> 'no'

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
  if condition then
    break
  end
end
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

Functions

Creating fns
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

Invoking fns
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

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({})

Data Type APIs

Global functions

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)
Strings
LUA
滚动查看更多
s = "Hello"

Concatenation

LUA
滚动查看更多
s .. " there" -- => Hello there

Commonly used methods

LUA
滚动查看更多
s:upper() -- => HELLO
s:lower() -- => hello
s:len()   -- => 5
s:find("o") -- => 5
s:reverse() -- => olleH

Sub

LUA
滚动查看更多
local s = "programming"
s:sub(3, 7) -- (extracts substring) => "ogram"

Gsub

LUA
滚动查看更多
s:gsub() --> (substitutes all matches)

Char

LUA
滚动查看更多
s = "ha"
s:rep(3) -- // repeats 3 times -> "hahaha"
local s_char = string.char(72, 101, 108, 108, 111)
print(s_char) -- "Hello"

Format

LUA
滚动查看更多
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."

Others

LUA
滚动查看更多
s:match()
s:gmatch()
s:dump()
s:byte()
placeholder
Math
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`.

LUA
滚动查看更多
math.tan(x)` -- => tangent of x
-- Returns the tangent of `x` (assumed to be in radians).

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

Misc

Classes

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
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

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572