当前位置:网站首页>Lua get started quickly
Lua get started quickly
2022-07-28 01:23:00 【Two blanks】
Catalog
String and number are converted to each other
Name variable
stay lua in , Assigning a variable is equivalent to naming a variable . Variables that are not known , The value of this variable is equal to nil( amount to c Linguistic NULL).


Be careful : stay lua The variable named in the default is a global variable . It can also be used in other files . Function and c Same language
If you want to make a variable local , Add... Before the variable local keyword .
local a = 10Assignment language can assign multiple variables :

number Types and operators
c There are many built-in types in languages ,int,char,float,double etc. , stay lua in , These types are unified number type .
lua Languages are also supported 16 Hexadecimal assignment and scientific counting .

Operator :lua Basic support c Operators in language , Like addition, subtraction, multiplication and division , power , Move left, move right, etc .
string type
Name string variable , Also by assigning a name , Single quotation marks or double quotation marks can be used for strings , also lua Escape character support .

Use "[[ ]]", It is also a name character , And the string is stored in the original , There will be no escape .

The connecting symbol of the string ,".."

String and number are converted to each other

If tonumber Conversion failure returns nil

Add a before the string variable '#' You can get the length of the string .

function
The format of the function is :


Return value , If the function does not return , Default return nil, There is a return , Return the corresponding value .


Function return value , Multiple values can also be returned

table Array
Numeric subscript
The use of numerical subscripts and c Arrays in language are similar , It can store numbers , character string , Another one table, Or functions . But starting from 1 It starts with a small mark .

If you access undefined table Subscript , The value is nil, But you can also assign values .

have access to '#' To get table Number of elements .

table The interface of
Plug in the interface insert
You can also use table Provided insert Interface , To insert elements , Or insert .

Remove interface remove
Remove the element at a certain position , And it will return the removed elements .

String subscript table
amount to c The structure of language , Can be in the form of an array ( Array of key Must be a string ) perhaps '.' Form access .
Subscripts are represented by strings .

Insert elements

Remove elements
Can't use table Of remove Interface , You can set the corresponding value of this string to nil.

Summarize two ways to remove elements : Empty or use table Interface remove
1. Numeric subscript , have access to remove And empty
2. String subscript , You can use empty .
overall situation table——_G
Global variables are stored in _G overall situation table in .

Be careful : Actually table It's also a global variable , His type is a table, So is the name table, It contains elements insert and remove function .

lua True and false in
lua There is a kind of bool type , Only true and false Two values .

stay lua There are only false and nil Said the false , Other expressions are true .0 Said really
Be careful :lua Logical operations in and,or You can directly put back the corresponding value .
and If it's true , Returns the last true value , If one is true and one is false , Return the corresponding false value , If it's all false , Return the first false value .
or If it's true , Returns the first true value , If one is true and one is false , Return the corresponding true value , If it's all false , Returns the last false value .
Short circuit evaluation can be realized by using this phenomenon .
such as :
Branch statement
use then Represents the branch statement to be executed later , use end Indicates the end of the branch statement .

Loop statement
lua There are three cycles , Namely for loop ,while Circulation and repeat loop .while and repeat Recycling is almost .
for loop
for Variable definitions , End value , step ( Do not write default is 1) do
Code block
end

Be careful :
1. for The variable scope defined in this line is only in for In circulation , But in for In the loop, you cannot modify for Circulation is meaningless .
2. And this variable can only be used in for In circulation Visit , It can't be modified . When making modifications , Xiang Dan and Zai for A new local variable is created in the loop .

3. have access to break keyword , No, continue keyword .

while loop
while Conditions do
sentence
Change of conditions
end
Support break

Add :
lua String and c Character arrays in languages are much like . have access to string Of char Function constitution lua String , use string Of byte Get a character in the string . And will 0 Save in string , It will not affect the length of the string .

边栏推荐
- 重新定义分析 - EventBridge 实时事件分析平台发布
- Cesium add light sweep
- 美光起诉联电窃密案宣判:联电被罚1亿元新台币,三名员工被判刑!
- Understanding of cap
- 安全检测风险
- 如何让数字零售承接起流量时代和留量时代的发展重任,或许才是关键所在
- 文件系统挂载
- Huawei responded to the US blockade of the supply chain: they still have to pay for 5g patents
- 容器网络硬核技术内幕 (7) 大海航行靠舵手
- Deepening the concept of linear algebra [23] 01 - points coordinate points and vectors vectors
猜你喜欢
随机推荐
Nokia announces cooperation with Broadcom to develop 5g chip
Node red interacts with tdengine
BigDecimal common API
Matlab drawing - points and vectors: method and source code of vector addition and subtraction
Use of swarm task task
Flutter--密码登录注册界面
System clock failure of database fault tolerance
Ford SUV "Mustang" officially went offline, safe and comfortable
C语言main函数传递参数
Swoole定时器
4月全球智能手机出货同比下滑41%,华为首次超三星成全球第一
Shenzhen Huaqiang announced that it plans to invest no more than 20million yuan in BYD semiconductor
BSP video tutorial issue 21: easy one key implementation of serial port DMA variable length transceiver, support bare metal and RTOS, including MDK and IAR, which is more convenient than stm32cubemx (
Arm中国夺权大战的背后:“独立”两年,仍难“自主”?
Unity Shader入门精要学习——基础纹理
Wentai technology acquired the remaining equity of ANSYS semiconductor and obtained unconditional approval
Operator depth anatomy
How to make digital retail undertake the development task of the era of traffic and retention may be the key
总投资近16亿元!乾照光电VCSEL、高端LED芯片项目正式开工
MySQL进阶--存储过程以及自定义函数









