当前位置:网站首页>Lua introductory case of actual combat 1234 custom function and the standard library function
Lua introductory case of actual combat 1234 custom function and the standard library function
2022-08-01 01:35:00 【Haibao 7】
– Example – Functions.
Function-related case customization
定义函数1-简单调用
– Define a function without parameters or return value.
案例如下
function myFirstLuaFunction()
print("My first lua function was called")
end
– Call myFirstLuaFunction.
myFirstLuaFunction()
-------- Output ------
My first lua function was called
定义函数2-带返回值
– Example – More functions.
– Define a function with a return value.
function mySecondLuaFunction()
return "string from my second function"
end
– Call function returning a value.
a=mySecondLuaFunction("string")
print(a)
-------- Output ------
string from my second function
定义函数3-多参数返回
– Define function with multiple parameters and multiple return values.
function myFirstLuaFunctionWithMultipleReturnValues(a,b,c)
return a,b,c,"My first lua function with multiple return values", 1, true
end
a,b,c,d,e,f = myFirstLuaFunctionWithMultipleReturnValues(1,2,"three")
print(a,b,c,d,e,f)
-------- Output ------
1 2 three My first lua function with multiple return values 1 true
定义函数4-全局变量与局部变量
– All variables are global in scope by default.
b=“global”
– To make local variables you must put the keyword ‘local’ in front.
function myfunc()
local b=" local variable"
a="global variable"
print(a,b)
end
调用函数
myfunc()
print(a,b)
-------- Output ------
global variable local variable
global variable global
定义函数5-格式化输出
function printf(fmt, ...)
io.write(string.format(fmt, ...))
end
printf("Hello %s from %s on %s\n",
os.getenv"USER" or "there", _VERSION, os.date())
-------- Output ------
Hello there from Lua 5.1 on 07/30/22 14:34:25
Define comments on multiple lines
-- Example 31 --[[
Standard Libraries
Lua has standard built-in libraries for common operations in
math, string, table, input/output & operating system facilities.
External Libraries
Numerous other libraries have been created: sockets, XML, profiling,
logging, unittests, GUI toolkits, web frameworks, and many more.
]]
-------- Output ------
标准函数库
标准数学库
– Math functions:
– math.abs, math.acos, math.asin, math.atan, math.atan2,
– math.ceil, math.cos, math.cosh, math.deg, math.exp, math.floor,
– math.fmod, math.frexp, math.huge, math.ldexp, math.log, math.log10,
– math.max, math.min, math.modf, math.pi, math.pow, math.rad,
– math.random, math.randomseed, math.sin, math.sinh, math.sqrt,
– math.tan, math.tanh
print(math.sqrt(9), math.pi)
-------- Output ------
3 3.1415926535898
标准库string
– String functions:
– string.byte, string.char, string.dump, string.find, string.format,
– string.gfind, string.gsub, string.len, string.lower, string.match,
– string.rep, string.reverse, string.sub, string.upper
print(string.upper("lower"),string.rep("a",5),string.find("abcde", "cd"))
-------- Output ------
LOWER aaaaa 3 4
标准库table
– Table functions:
– table.concat, table.insert, table.maxn, table.remove, table.sort
a={2}
table.insert(a,3);
table.insert(a,4);
table.sort(a,function(v1,v2) return v1 > v2 end)
for i,v in ipairs(a) do print(i,v) end
-------- Output ------
1 4
2 3
3 2
标准输入输出IO
– IO functions:
– io.close , io.flush, io.input, io.lines, io.open, io.output, io.popen,
– io.read, io.stderr, io.stdin, io.stdout, io.tmpfile, io.type, io.write,
– file:close, file:flush, file:lines ,file:read,
– file:seek, file:setvbuf, file:write
print(io.open("file doesn't exist", "r"))
-------- Output ------
nil file doesn’t exist: No such file or directory 2
System function configurationOS库
– OS functions:
– os.clock, os.date, os.difftime, os.execute, os.exit, os.getenv,
– os.remove, os.rename, os.setlocale, os.time, os.tmpname
print(os.date())
-------- Output ------
07/30/22 14:39:54
扩展库支持
– Lua has support for external modules using the ‘require’ function
– INFO: A dialog will popup but it could get hidden behind the console.
require( "iuplua" )
ml = iup.multiline
{
expand="YES",
value="Quit this multiline edit app to continue Tutorial!",
border="YES"
}
dlg = iup.dialog{ml; title="IupMultiline", size="QUARTERxQUARTER",}
dlg:show()
print("Exit GUI app to continue!")
iup.MainLoop()
-------- Output ------
failed to load & run sample code
Configuration is not over yet.
```css
-- Example 38 --[[
To learn more about Lua scripting see
Lua Tutorials: http://lua-users.org/wiki/TutorialDirectory
"Programming in Lua" Book: http://www.inf.puc-rio.br/~roberto/pil2/
Lua 5.1 Reference Manual:
Start/Programs/Lua/Documentation/Lua 5.1 Reference Manual
Examples: Start/Programs/Lua/Examples
]]
-------- Output ------
Press 'Enter' key for next example
边栏推荐
- Js replication
- 链式编程、包、访问权限
- JVM面试题总结(持续更新中)
- MYSQL-Batch insert data
- 网关gateway跨域
- MYSQL master-slave replication
- 【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
- MYSQL Keyword Explain Analysis
- What is the meaning of JS timestamp?Know SQL will consider to add a timestamp, JS timestamp for the scene?
- Inheritance Considerations
猜你喜欢

【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard

MYSQL经典面试题

从零造键盘的键盘超级喜欢,IT人最爱

leetcode:1562. 查找大小为 M 的最新分组【模拟 + 端点记录 + 范围合并】
![ROS2 series of knowledge (4): understand the concept of [service]](/img/14/8de92a89d9c4b6476ac37408bc7788.png)
ROS2 series of knowledge (4): understand the concept of [service]

SC7A20(士兰微-加速度传感器)示例

Cmake introductory study notes

STK8321 I2C(昇佳-加速度传感器)示例

MYSQL主从复制

RTL8762DK 点灯/LED(三)
随机推荐
更换树莓派内核
手写二叉查找树及测试
C string array reverse
软考高级系统架构设计师系列之:系统开发基础知识
【Cryptography/Cryptanalysis】Cryptanalysis method based on TMTO
VPGNet
【历史上的今天】7 月 31 日:“缸中之脑”的提出者诞生;Wi-Fi 之父出生;USB 3.1 标准发布
STK8321 I2C(昇佳-加速度传感器)示例
普通用户无法访问hgfs目录
500 miles
Fat interface in JQESAP system
Flink 部署和提交job
JVM面试题总结(持续更新中)
ROS2系列知识(4): 理解【服务】的概念
Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
MYSQL经典面试题
Soft Exam Senior System Architect Series: Basic Knowledge of Information Systems
Simple vim configuration
链式编程、包、访问权限
带wiringPi库在unbutu 编译 并且在树莓派运行