LUA学习笔记(第18-20章)
数学库
print(math.pi)-->π
print(math.huge)-->Lua中表示的最大数字
--[[
3.1415926535898
1.#INF
]]
print(math.rad(90))-->转换成弧度
print(math.deg(math.pi))-->转换成角度
math.random()用于生成伪随机数
不带参数,它将返回【0, 1]内均匀分布的伪随机实数
带参数n(整数),它将返回【1, n】内均匀分布的伪随机整数
带参数m,n(整数),它将返回【m, n】内均匀分布的伪随机整数
math.randomseed用于设置伪随机数生成器的种子数
math.randomseed(os.time())
math.randomseed(os.time())
for i=1,5 do
print(math.random(6))
end
table库
arr = {}
for i=1,10 do
arr[i] = i * 10
end
print("the length of arr is " .. #arr)
for i,v in ipairs(arr) do
print("arr[" .. i .. "] = " .. v)
end
lines = {luaH_set = 10, luaH_get = 24, luaH_present = 48}
for i,v in pairs(lines) do
print(i, v)
end
插入
arr = {}
for i=1,10 do
arr[#arr + 1] = i * 10
end
print(table.concat(arr, " : ", 1, 10))
遍历嵌套数组
function printer(arr)
if type(arr) ~= "table" then
print(arr)
return
end
for i=1,#arr do
printer(arr[i])
end
end s = {{"a", "nice"}, "and", {"long"}, {"day"}}
--printer(s)
printer(s)
function printer(arr)
if type(arr) ~= "table" then
return arr
end
local res = {}
for i=1,#arr do
res[i] = printer(arr[i])
end
return table.concat(res, " ")
end local s = {{"a", "nice"}, "and", {"long"}, {"day"}} print(printer(s))
排序
table.sort(arr)
print(unpack(arr))
if x < y then
return true
else
return false
end
end
arr = {2, 5, 1, 3, 4}
table.sort( arr, sortfunction)
print(unpack(arr))
lua中无法对table的索引排序,只能将key保存在数组,再对数组排序。
lines = {
luaH_set = 10,
luaH_get = 24,
luaH_present = 48,
}
arr = {}
for key in pairs(lines) do
arr[#arr + 1] = key
end
print(unpack(arr))
table.sort(arr)
print(unpack(arr))
LUA学习笔记(第18-20章)的更多相关文章
- LUA学习笔记(第5-6章)
x = a or b 如果a为真则x = a 如果a为假则x = b print(a .. b) 任何非nil类型都会被连接为字符串,输出 多重返回值 local s,e = string.find( ...
- LUA学习笔记(第1-4章)
需要一种简单的脚本语言来代替批处理,它需要足够小巧,同时功能上也应该足够强劲,自然选择了LUA语言. 第一章 Hello World print('Hello World') print(" ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十三章:计算着色器(The Compute Shader)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十三章:计算着色器(The Compute Shader) 代码工程 ...
- [转]LUA 学习笔记
Lua 学习笔记 入门级 一.环境配置 方式一: 1.资源下载http://www.lua.org/download.html 2.用src中的源码创建了一个工程,注释调luac.c中main函数,生 ...
- 《jQuery权威指南》学习笔记之第2章 jQuery选择器
2.1 jQuery选择器概述 2.1.1 什么使选择器 2.1.2 选择器的优势: 代码更简单,完善的检测机制 1.代码更简单 示例2-1 使用javascript实现隔行变色 < ...
- Lua学习笔记4. coroutine协同程序和文件I/O、错误处理
Lua学习笔记4. coroutine协同程序和文件I/O.错误处理 coroutine Lua 的协同程序coroutine和线程比较类似,有独立的堆栈.局部变量.独立的指针指令,同时又能共享全局变 ...
- Lua学习笔记:面向对象
Lua学习笔记:面向对象 https://blog.csdn.net/liutianshx2012/article/details/41921077 Lua 中只存在表(Table)这么唯一一种数据结 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第二十三章:角色动画 学习目标 熟悉蒙皮动画的术语: 学习网格层级变换 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二)
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第七章:在Direct3D中绘制(二) 代码工程地址: https:/ ...
随机推荐
- React Hooks 完全指南,读React作者博文感悟(2W字精华)
阅读 facebook大佬:Dan Abramov 的文章颇有感悟 大佬 github地址 https://github.com/gaearon 重点总结 useEffect 是同步的 状态是捕获的当 ...
- 机器学习——EM算法与GMM算法
目录 最大似然估计 K-means算法 EM算法 GMM算法(实际是高斯混合聚类) 中心思想:①极大似然估计 ②θ=f(θold) 此算法非常老,几乎不会问到,但思想很重要. EM的原理推导还是蛮复杂 ...
- sg函数的变形 - 可以将一堆石子分开
Nim is a two-player mathematic game of strategy in which players take turns removing objects from di ...
- JMeter——分布式压测
一.Jmeter4.0分布式压测准备工作 压测注意事项 the firewalls on the systems are turned off or correct ports ...
- [apue] 使用 Ctrl+S停止输出而不用挂起前台进程
之前一直知道使用 Ctrl+Z 挂起前台进程来阻止进程运行,之后可以再通过 shell 的作业控制 (jobs / fg N) 来将后台进程切换为前台,从而继续运行. 最近学到一种新的方法,对于不停有 ...
- [bzoj4447] [loj#2010] [Scoi2015] 小凸解密码
Description 小凸得到了一个密码盘,密码盘被等分成 \(N\) 个扇形,每个扇形上有一个数字(0-9),和一个符号("+"或"*") 密码盘解密的方法 ...
- 关于Hive中case when不准使用子查询的解决方法
在公司用Hive实现个规则的时候,遇到了要查询某个字段是否在另一张表中,大概情况就是 A表: id value1 value2 1 100 0 2 101 1 3 102 1 B表: value1 1 ...
- 「 深入浅出 」集合List
第一篇文章 「 深入浅出 」java集合Collection和Map 主要讲了对集合的整体介绍,本篇文章主要讲List相对于Collection新增的一些重要功能以及其重要子类ArrayList.Li ...
- HUAWEI MateBook Fn 功能键/热键切换、设置方法
原文地址:https://club.huawei.com/thread-13130964-1-1.html HUAWE MateBook E/X/D的F1.F2 等键默认是热键优先.在热键模式下,要想 ...
- NOI2.6 8782: 乘积最大
描述 今年是国际数学联盟确定的"2000--世界数学年",又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好 ...