Lua learning (1)

LUA language

1. What is lua?

Lua is a lightweight and compact programming language designed to be embedded into applications.

This provides extensions to the application.

It uses standard C and is open to the outside world in the form of source code.

1.1 Preparation

First download lua -> lua official website download

After reaching the official website, click download

Insert image description here

Select the compressed file, which contains the source code that we need to compile.
Insert image description here

Here I am using a linux system.

  • Transfer the compressed package to the virtual machine. I use Xftp and Xshell to find the directory where the compressed package is stored.
tar - zxvf lua-5.4.6.tar.gz
  • After decompression, you will reach the folder with the makefile.
make linux
  • It’s basically done here.
  • After make is completed, two exe files will be generated, namely lua and luac.
  • Among them, lua can directly execute lua scripts
  • luac compiles lua to generate an .out file and then runs the .out file

During this period, I encountered a problem that I did not have the make command. Just enter the following command:

yum -y install gcc automake autoconf libtool make yum install gcc gcc-c++

2. Basic grammar

2.1 Notes
  • Single line comments
--
  • Multi-line comments (the shortcut key is generally ctrl + shift + ?)
--[[
 多行注释
 多行注释
 --]]
2.2 Identifier

Lua can use _, numbers, and letters to represent variables but cannot use special symbols to represent variables. The following are all correct notation representations.

mohd         zara      abc     move_name    a_123
myname50     _temp     j       a23b9        retVal
2.3 Keywords

First, keywords are reserved by Lua and cannot be used as user-defined identifiers.

and break do else
elseif end false for
function if in local
nil not or repeat
return then true until
while goto

By general convention, names starting with an underscore followed by a string of uppercase letters (such as _VERSION) are reserved for Lua internal global variables.

2.4 Global variables

By default variables are global, and local variables appear in methods.

3. Data type

There are 8 basic types in Lua: nil, boolean, number, string, userdata, function, thread and table.

type of data describe
nil This is the simplest. Only the value nil belongs to this class, which represents an invalid value (equivalent to false in a conditional expression).
boolean Contains two values: false and true.
number Represents a real floating-point number of type double
string A string is represented by a pair of double quotes or single quotes
function Function written in C or Lua
userdata Represents any C data structure stored in a variable
thread Represents an independent line of execution for executing coroutines
table A table in Lua is actually an "associative array", and the index of the array can be a number, string, or table type. In Lua, table creation is accomplished through "constructive expressions". The simplest constructed expression is {}, which is used to create an empty table.

4. Variables

There are three types of Lua variables: global variables, local variables, and fields in tables.

Adding local variables means that local variables can only act on this file.

And if a variable with local added is defined in a method, then this variable is only visible in this method.

a = 5               -- 全局变量
local b = 5         -- 局部变量

function joke()
    c = 5           -- 全局变量
    local d = 6     -- 局部变量
end

-- joke()  要先进行调用才可以直接使用
-- print(c,d) --5 nil  
print(c,d) -- nil nil 
4.1 Assignment statement
site  = {
    
    }

site["key"] = "www.baidu.com"
print(site.key)
print(site["key"])
-- 当索引为字符串类型时的一种简化写法
-- 采用索引访问本质上是一个类似这样的函数调用
-- www.baidu.com
-- www.baidu.com

Lua can assign values ​​to multiple variables at the same time. Each element of the variable list and value list is separated by commas. The values ​​on the right side of the assignment statement will be assigned to the variables on the left in turn.

a,b =10,"dada"
print(a,b)
c,d,e,f=10
print(c,d,e,f)
--变量个数 > 值的个数             按变量个数补足nil
--变量个数 < 值的个数             多余的值会被忽略

5. Loop

5.1 while loop

Let the program repeatedly execute certain statements when the condition is true. Before executing the statement, it will first check whether the condition is true.

a=0
while(a < 10)
    do
    print("a的值是:",a)
    a = a+1
end
5.2 for loop
for var=exp1,exp2,exp3 do  
    <执行体>  
end 
var 从 exp1 变化到 exp2,每次变化以 exp3 为步长递增 var,并执行一次 "执行体"。exp3 是可选的,如果不指定,默认为1for i = 20, 10,-2 do
	print(i)
end

5.3 Generic for loop

The generic for loop traverses all values ​​through an iterator function, similar to the foreach statement in Java.

a= {
    
    "one","two","three","four","five"}
for i, v in ipairs(a) do
    print(i,v)
end
5.4 repeat until loop

The conditional statements of for and while loops are evaluated at the beginning of the current loop execution, while the conditional statements of repeat...until loops are evaluated after the current loop ends.

b =0
repeat
    print(b)
    b = b+1
until(b >10 )
5.5 break statement

The break statement is inserted into the loop body to exit the current loop or statement and start the script to execute the following statement.

a = 10

--[ while 循环 --]
while( a < 20 )
do
   print("a 的值为:", a)
   a=a+1
   if( a > 15)
   then
      --[ 使用 break 语句终止循环 --]
      break
   end
end

6. Process control

6.1 if statement

Flow control statements are set by the program setting one or more conditional statements. Execute the specified program code when the condition is true, and execute other specified code when the condition is false.

--[ 0 为 true ]
if(0)
then
    print("0 为 true")
end
6.2 if else statement

The Lua if statement can be used in conjunction with the else statement, and the else statement code block is executed when the if conditional expression is false.

if(布尔表达式)
then
   --[ 布尔表达式为 true 时执行该语句块 --]
else
   --[ 布尔表达式为 false 时执行该语句块 --]
end


a =10
if(a >6)then
    print("我比6大")
else
    print("我比6小")
end

Lua considers false and nil to be false, and true and non-nil to be true. It should be noted that 0 is true in Lua.

6.3 if else if statement

If … else if…else statements are used together. When the if conditional expression is false, the elseif…else statement code block is executed to detect multiple conditional statements.

if( 布尔表达式 1)
then
   --[ 在布尔表达式 1 为 true 时执行该语句块 --]

elseif( 布尔表达式 2)
then
   --[ 在布尔表达式 2 为 true 时执行该语句块 --]

elseif( 布尔表达式 3)
then
   --[ 在布尔表达式 3 为 true 时执行该语句块 --]
else 
   --[ 如果以上布尔表达式都不为 true 则执行该语句块 --]
end

Guess you like

Origin blog.csdn.net/m0_52021450/article/details/132556063