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 Types | Definition | Syntax | Example of Use |
---|---|---|---|
Number | A numeric value including positive values, negative values, and decimal values. | 10 , 3.5 , -4 |
To store how many followers you have on Instagram. |
String | A 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. |
Nil | A representation for the absence of a value. If there is no value, it is nil. | nil |
To 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
1 2 3 4 5 |
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 stringtonumber()
: converts a variable to number
Operations
Arithmetic Operators
Operator Name | Description | Syntax | Example | Result |
---|---|---|---|---|
Addition | Adds two numbers | x + y | 5 + 2 | 7 |
Subtraction | Subtracts two numbers | x – y | 5 – 2 | 3 |
Multiplication | Multiplies two numbers | x * y | 5 * 2 | 10 |
Division | Divides two numbers | x / y | 5 / 2 | 2.5 |
Exponential | Takes the exponent of two numbers | x ^ y | 5 ^ 2 | 25 |
Remainder (also known as modulo or modulus) |
Gives us the remaining leftover number after we’ve divided two numbers. | x % y | 5 % 2 | 1 |
Negation | Reverses the sign value of the number. | -x | -(3+2) | -5 |
Logical Operators
Operator | Syntax | Description |
---|---|---|
and |
A and B |
true if values A and B are true , false otherwise. |
or |
A or B |
true if at least one of A or B are true , false otherwise. |
not |
not A |
true 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 (
..
).
1 2 3 |
print("Hello World, ".."LUA!") --output: Hello World, LUA! |
Syntax
Comment
- single line comment:
1 2 |
-- This is the comment line. |
- Code block comment:
1 2 3 4 |
--[[ This is the comment block. ]] |
Conditionals
If statement
1 2 3 4 |
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 EqualB
.
If-else statement
1 2 3 4 5 6 |
If boolean_experssion then -- do something else -- do something else end |
If-elseif statement
1 2 3 4 5 6 7 8 |
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
1 2 3 4 5 |
-- 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)
1 2 3 4 5 6 7 |
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
1 2 3 4 |
function sayHello(name) return ("Hello World, "..name) end |
Built-in functions
1 2 3 4 5 |
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
1 2 |
sudo apt install lua5.3 |
Run LUA script
1 2 |
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.
最新评论
感谢博主,让我PyTorch入了门!
博主你好,今晚我们下馆子不?
博主,你的博客用的哪家的服务器。
您好,请问您对QNN-MO-PYNQ这个项目有研究吗?想请问如何去训练自己的数据集从而实现新的目标检测呢?
where is the source code ? bomb1 188 2 8 0 0 hello world 0 0 0 0 0 0 1 1 9?5
在安装qemu的过程中,一定在make install 前加入 sudo赋予权限。
所以作者你是训练的tiny-yolov3还是yolov3...
很有用