当前位置:网站首页>lua入门案例实战123DIY
lua入门案例实战123DIY
2022-07-31 22:50:00 【海宝7号】
代码及如下
– Example 7 – Keywords.
– Lua reserved words are: and, break, do, else, elseif,
– end, false, for, function, if, in, local, nil, not, or,
– repeat, return, then, true, until, while.
– Keywords cannot be used for variable names,
– ‘and’ is a keyword, but AND is not, so it is a legal variable name.
AND=3
print(AND)
-------- Output ------
3
Press 'Enter' key for next example
– Example 8 – Strings.
a=“single ‘quoted’ string and double “quoted” string inside”
b=‘single ‘quoted’ string and double “quoted” string inside’
c= [[ multiple line
with ‘single’
and “double” quoted strings inside.]]
print(a)
print(b)
print
-------- Output ------
single ‘quoted’ string and double “quoted” string inside
single ‘quoted’ string and double “quoted” string inside
multiple line
with ‘single’
and “double” quoted strings inside.
Press 'Enter' key for next example
– Example 9 – Assignments.
– Multiple assignments are valid.
– var1,var2=var3,var4
a,b,c,d,e = 1, 2, “three”, “four”, 5
print(a,b,c,d,e)
-------- Output ------
1 2 three four 5
Press ‘Enter’ key for next example
– Example 10 – More Assignments.
– Multiple assignments allows one line to swap two variables.
print(a,b)
a,b=b,a
print(a,b)
-------- Output ------
1 2
2 1
Press ‘Enter’ key for next example
– Example 11 – Numbers.
– Multiple assignment showing different number formats.
– Two dots (…) are used to concatenate strings (or a
– string and a number).
a,b,c,d,e = 1, 1.123, 1E9, -123, .0008
print(“a=”…a, “b=”…b, “c=”…c, “d=”…d, “e=”…e)
-------- Output ------
a=1 b=1.123 c=1000000000 d=-123 e=0.0008
Press ‘Enter’ key for next example
– Example 12 – More Output.
– More writing output.
print “Hello from Lua!”
print(“Hello from Lua!”)
-------- Output ------
Hello from Lua!
Hello from Lua!
Press ‘Enter’ key for next example
– Example 13 – More Output.
– io.write writes to stdout but without new line.
io.write(“Hello from Lua!”)
io.write(“Hello from Lua!”)
– Use an empty print to write a single new line.
print()
-------- Output ------
Hello from Lua!Hello from Lua!
Press ‘Enter’ key for next example
– Example 14 – Tables.
– Simple table creation.
a={} – {} creates an empty table
b={1,2,3} – creates a table containing numbers 1,2,3
c={“a”,“b”,“c”} – creates a table containing strings a,b,c
print(a,b,c) – tables don’t print directly, we’ll get back to this!!
-------- Output ------
table: 00BB6AE0 table: 00BB6BF8 table: 00BB6C20
Press ‘Enter’ key for next example
– Example 15 – More Tables.
– Associate index style.
address={} – empty address
address.Street=“Wyman Street”
address.StreetNumber=360
address.AptNumber=“2a”
address.City=“Watertown”
address.State=“Vermont”
address.Country=“USA”
print(address.StreetNumber, address[“AptNumber”])
-------- Output ------
360 2a
Press ‘Enter’ key for next example
– Example 17 – if else statement.
b=“happy”
if b==“sad” then
print(“b is sad”)
else
print(“b is not sad”)
end
-------- Output ------
b is not sad
Press ‘Enter’ key for next example
– Example 18 – if elseif else statement
c=3
if c1 then
print(“c is 1”)
elseif c2 then
print(“c is 2”)
else
print("c isn’t 1 or 2, c is "…tostring)
end
-------- Output ------
c isn’t 1 or 2, c is 3
Press ‘Enter’ key for next example
– Example 19 – Conditional assignment.
– value = test and x or y
a=1
b=(a==1) and “one” or “not one”
print(b)
– is equivalent to
a=1
if a==1 then
b = “one”
else
b = “not one”
end
print(b)
-------- Output ------
one
one
Press ‘Enter’ key for next example
– Example 20 – while statement.
a=1
while a~=5 do – Lua uses ~= to mean not equal
a=a+1
io.write(a…" ")
end
-------- Output ------
2 3 4 5
Press ‘Enter’ key for next example
– Example 21 – repeat until statement.
a=0
repeat
a=a+1
print(a)
until a==5
-------- Output ------
1
2
3
4
5
Press ‘Enter’ key for next example
– Example 22 – for statement.
– Numeric iteration form.
– Count from 1 to 4 by 1.
for a=1,4 do io.write(a) end
print()
– Count from 1 to 6 by 3.
for a=1,6,3 do io.write(a) end
-------- Output ------
1234
14
Press ‘Enter’ key for next example
– Example 23 – More for statement.
– Sequential iteration form.
for key,value in pairs({1,2,3,4}) do print(key, value) end
-------- Output ------
1 1
2 2
3 3
4 4
Press ‘Enter’ key for next example
– Example 24 – Printing tables.
– Simple way to print tables.
a={1,2,3,4,“five”,“elephant”, “mouse”}
for i,v in pairs(a) do print(i,v) end
-------- Output ------
1 1
2 2
3 3
4 4
5 five
6 elephant
7 mouse
Press ‘Enter’ key for next example
– Example 25 – break statement.
– break is used to exit a loop.
a=0
while true do
a=a+1
if a==10 then
break
end
end
print(a)
-------- Output ------
10
Press ‘Enter’ key for next example
边栏推荐
- Shell common scripts: Nexus batch upload local warehouse enhanced version script (strongly recommended)
- -xms -xmx(information value)
- flowable workflow all business concepts
- Flutter教程之 01配置环境并运行demo程序 (教程含源码)
- #yyds dry goods inventory# Interview must brush TOP101: the entry node of the ring in the linked list
- Summary of the classic drawing method of histogram
- 如何导入 Golang 外部包并使用它?
- Network security - crack WiFi through handshake packets (detailed tutorial)
- 景区手绘地图的绘制流程
- Federated Learning: Multi-source Knowledge Graph Embedding in Federated Scenarios
猜你喜欢

ICML2022 | 深入研究置换敏感的图神经网络

手写一个简单的web服务器(B/S架构)

(26) About menu of the top menu of Blender source code analysis
![[NLP] What is the memory of the model!](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[NLP] What is the memory of the model!

Pytest初体验

Shell常用脚本:Nexus批量上传本地仓库增强版脚本(强烈推荐)

flowable workflow all business concepts

In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved

Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic

Audio alignment using cross-correlation
随机推荐
Several methods for deleting specified elements in Golang slices
SQL注入 Less54(限制次数的SQL注入+union注入)
如何导入 Golang 外部包并使用它?
日常--Kali开启SSH(详细教程)
[QNX Hypervisor 2.2 User Manual]9.16 system
#yyds干货盘点# 面试必刷TOP101:链表中环的入口结点
TypeScript 的组件
Interview assault 69: TCP reliable?Why is that?
消息队列消息存储设计(架构实战营 模块八作业)
Shell common script: Nexus batch upload local warehouse script
Go1.18 upgrade function - Fuzz test from scratch in Go language
MySQL数据库‘反斜杠\’ ,‘单引号‘’,‘双引号“’,‘null’无法存储
Daily--Kali opens SSH (detailed tutorial)
PHP三元(三目)运算符
GateWay implements load balancing
22年8月推广大使额外奖励规则
Design of Fire and Anti-theft System Based on Single Chip GSM
[QNX Hypervisor 2.2 User Manual]9.14 set
面试突击69:TCP 可靠吗?为什么?
Recognize anomalies (you will understand after reading this)