LinMao's Blog
学习科研记录与分享!

LUA Basics

A quick note of LUA for later reference.

Lua is a powerful and intuitive general-purpose programming language developed in 1993 by a team of scrappy professors at Pontifical Catholic University of Rio de Janeiro, in Brazil.

Much of the inspiration for Lua came from the languages SOL (Simple Object Language) and DEL (data-entry language). In Portuguese, Sol means “sun” and Lua means “moon”.

Data Types

Basic Data Types

Data TypesDefinitionSyntaxExample of Use
NumberA numeric value including positive values, negative values, and decimal values.103.5-4To store how many followers you have on Instagram.
StringA sequence of individual characters inside quotations. It can be letters, spaces, numbers, or symbols."This is a string"
'I have 5 cats!'
To store your username on a website.
Boolean
(boo·lee·uhn)
A value that only has two possible values: true or false.true
false
To indicate whether you have dark mode on or not.
NilA representation for the absence of a value. If there is no value, it is nil.nilTo indicate an empty box on a fillable form.

Note: There are also an additional four complex data types: Tables, Functions, Userdata, and Threads.

type(): get the data type of a variable.

Dynamic data type: Since variables in Lua do not have a type — it is just a container for data — you can reassign a value of any data type to a variable.

Type Convert

print("100" + 5)
--output: 105, "100" is first converted to number
print("100" + 5 .. 6)
--output:1056, 105 and 6 are converted to string
  • tostring() : converts a variable to string
  • tonumber(): converts a variable to number

Operations

Arithmetic Operators

Operator NameDescriptionSyntaxExampleResult
AdditionAdds two numbersx + y5 + 27
SubtractionSubtracts two numbersx - y5 - 23
MultiplicationMultiplies two numbersx * y5 * 210
DivisionDivides two numbersx / y5 / 22.5
ExponentialTakes the exponent of two numbersx ^ y5 ^ 225
Remainder
(also known as modulo or modulus)
Gives us the remaining leftover number after we’ve divided two numbers.x % y5 % 21
NegationReverses the sign value of the number.-x-(3+2)-5

Logical Operators

OperatorSyntaxDescription
andA and Btrue if values A and B are true, false otherwise.
orA or Btrue if at least one of A or B are true, false otherwise.
notnot Atrue if A is false and false if A is true.

Order of Operations

The list below shows what operators are evaluated first from top to bottom:

  • ^
  • not
  • *, /
  • +, -
  • <, >, <=, >=, ~=, ==
  • and
  • or

Like other programming language, LUA can use parentheses () to change the order of operations.

String Concatenation

  • concatenation operator (..).
print("Hello World, ".."LUA!")
--output: Hello World, LUA!

Syntax

Comment

  • single line comment:
-- This is the comment line.
  • Code block comment:
--[[
This is the comment block.
]]

Conditionals

If statement

If boolean_expression then
  -- do something
end
  • Comparison Operatoers, others are the same as Python or C/C++ except for: A ~= B: A doesn’t Equal B.

If-else statement

If boolean_experssion then
  -- do something
else
  -- do something else
end

If-elseif statement

If boolean_experssion_1 then
  -- do something
elseif boolean_experssion_2 then
  -- do something else
else
  -- do something else
end

Functions

Declare functions

  • Declare a function by using function keyword
-- A function that print Hello World!
function sayHello()
  print("Hello World!")
end -- end of the function
  • Declare a function with parameters and call the function with arguments (parameter is in the definition while argument is real value)
function sayHello(name) -- name is the parameter
  print("Hello World, "..name)
end

-- call the function
sayHello("LUA")  -- "LUA" is the argument
  • Declare a function with return
function sayHello(name)
  return ("Hello World, "..name)
end

Built-in functions

string.upper("hello") -- returns HELLO, more string functions https://www.codecademy.com/resources/docs/lua/strings
math.min(100, 250) -- returns 100
math.random() -- can return any decimal between 0 and 1
math.random(0, 100) -- can return any number between 0 and 100, including 0 or 100.

Run Lua on ubuntu terminal

Install LUA

sudo apt install lua5.3

Run LUA script

lua script.lua

Some thoughs

LUA is quite similar to Python, I can get a baisc command of LUA with one hour because of the experience of Python programming. Although there're some small differences: 1, comment syntax; 2, not equal operator (~=); 3, type convert and string concatenation, 4, don't distinguish int and float; 5, no need to import built-in module, etc. Other than those small differences, I guess a Python programmer can easily have a good command of LUA within a short time, some Python codes are even able to directly run with LUA without any change.

Reference

LUA Getting started

Lua 5.4 Reference Manual

赞(0) 打赏
转载请注明出处:LinMao's Blog(林茂的博客) » LUA Basics

评论 抢沙发

静态归档版本,评论功能已关闭。
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

LinMao's Blog(林茂的博客)

了解更多联系我们

觉得文章有用就打赏一下作者吧~

支付宝扫一扫打赏

支付宝

微信扫一扫打赏

微信