Lua language novice simple tutorial

Time:2021-10-18

1、 Foreword

Lua is a lightweight and compact scripting language, which is written in standard C language and open in the form of source code. Its design purpose is to be embedded in the application, so as to provide flexible expansion and customization functions for the application.

Lua can be applied in game development, independent application scripts, web application scripts, extensions and database plug-ins, security systems and other scenarios.

The purpose of my study is to use Lua scripts in Web Applications (nginx and redis).

characteristic

  • Lua script can be easily called by C / C + + code, or call C / C + + functions in turn.
  • Lua is written in standard C. the code is concise and beautiful. It can be compiled and run on almost all operating systems and platforms.
  • Provides comprehensive functionality with minimal size and simple syntax. [a complete Lua interpreter is only 200K, and Lua is the fastest of all script engines]
  • Implement a concise interface with the host language
  • Become a language that can be easily embedded in other languages.

characteristic

  • Support object-oriented programming
  • Lightweight
  • Extensible
  • Functional programming
  • Automatic memory management
  • General type table – table, which can realize array, hash table, set and object
  • Support closure
  • Support multithreading
  • Support interactive programming

2、 Installation

be careful:Make sure that readLine and readLine devel are installed on your system before installing Lua. If not, typeyum install -y readline readline-devel Install.

?
1
2
3
4
curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
tar zxf lua-5.3.4.tar.gz
cd lua-5.3.4
make linux test

Test, type Lua – V on the command line:

?
1
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio

The Lua version description is displayed and installed correctly.

3、 Operation mode

Like other scripting languages, we need to write Lua code in a file with the suffix. Lua.

When you run the code for this file, just type Lua xx.lua on the command line.

4、 Grammar

4.1 data type

Lua contains eight basic data types: Nil, Boolean, number, string, UserData, function, thread and table.

 

data type explain
nil Indicates an invalid value and false in a conditional expression.
boolean Boolean value, including true and false values.
number Represents a real floating-point number of double type.
string Represents a string enclosed in double or single quotes.
userdata Represents any C data structure stored in variables
function Represents a function written in C or Lua
thread Represents an independent thread for execution, which is used to execute cooperative programs.
table Represents an associative array. The array index can be a number or a string.

 

For example:

?
1
2
a=10
str="hello world"

There is no need to declare the variable type. We can judge the variable type through type ().

be careful:

Lua variables have three types: global variables, local variables and fields in the table;

By default, variables declared anywhere are global variables. Variables modified by local are local variables;

The default value of the variable is nil.

4.2 operators

1) Assignment operator

?
1
2
3
str="hello".."world" --Connect string through
a,b=10,15 -- a=10,b=15
c,d,e=1,2 -- c=1,b=2,e=nil

2) Arithmetic operator

Similar to other programming languages.

?
1
2
3
4
5
6
7
8
a,b=10,15
C = a + B -- plus sign
D = A-B -- minus sign
E = a * B -- multiplication sign
F = A / B -- division sign
G = a% B -- remainder
H = a ^ 2 -- power
I = - a -- minus sign

3) Relational operator

Similar to other programming languages.

?
1
2
3
4
5
a,b=10,15
Print (a > b) -- greater than
Print (a < b) -- less than
Print (a = = b) -- equal to
Print (a ~ = b) -- not equal to

4) Logical operator

?
1
2
3
print(true and false)-- and
print(true or false)-- or
print(not true)-- not

5) Other operators

 

Operator explain
.. Connect two strings
# Returns the length of a string or table, such as # “Lua”

 

4.3 process control

1) There are three ways to write conditional judgment.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- if sentence
if(true)
then
 print("hello")
end
-- if .. else sentence
a,b=10,15
if(a>b)
then
 print(a)
else
 print(b)
end
-- if nesting
c=20
if(c>10)
then
 if(c<30)
 then
 print(c)
 end
end

2) Loop, there are four ways to write.

 

type explain
While loop When the condition is true, let the program execute the statement repeatedly.
For loop Execute the specified statement repeatedly, and the number of repetitions is controlled in for. You can traverse numbers and generics.
repeat .. until Repeat the loop until the specified condition is true.
loop nesting Loop embeds multiple loop statements.

 

For example:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- while loop
a=10
while(a>0)
do
 print(a)
 a=a-1
end
-- for Loop, traversing numbers
for a=1,10,1 do
 print(a)
end
--Repeat.. until loop
a=10
repeat
 print(a)
 a=a-1
until(a<1)

4.4 array

Lua array size is not fixed and the subscript starts from 1.

?
1
2
3
4
5
6
7
8
9
arr={"h","e","l","l","o"}
--Here, the traversal number method is used
for index=1,#arr do
 print(arr[index])
end
--Traversal generics are used here
for i,v in ipairs(arr) do
 print(i,v)
end

4.5 function

?
1
2
3
4
5
6
7
8
9
10
11
12
--Case 1
function calc(a,b,c)
 return a+b+c
end
result=calc(1,2,3)
print(result)
--Case 2
function getCalc(a,b,c)
 return a,b,c
end
r1,r2,r3=getCalc(1,2,3)
print(r1,r2,r3)

Functions in Lua allow multiple values to be returned.

4.6 table

Table is a data structure in Lua, which is similar to map type in Java or JSON object in JavaScript.

Lua table uses associative arrays. We can use any type of value as the index of the array, but it cannot be nil.

Lua table size is not fixed.

?
1
2
3
4
5
6
person={}
person.name="jack"
person.age=20
print(person[1])
print(person.name)
print(person["age"])

4.7 modules and packages

The module is similar to an encapsulation library. Starting from Lua 5.1, Lua has added a standard module management mechanism, which can put some common code in one file and call it elsewhere in the form of API interface, which is conducive to code reuse and reduce code coupling.

Lua’s module is a table composed of known elements such as variables and functions.

For example:

Create a file named module.lua.

?
1
2
3
4
5
module={}
module.index=1
function module.sum(a,b)
 return a+b
end

Import another file:

?
1
2
3
4
5
--Here module is the file name
require "module"
--Here, module is the name defined in the import module
print(module.index)
print(module.sum(1,2))

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message. Thank you for your support for developepper.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]