Lua 标准库提供了一系列有用的函数和模块,可以帮助我们更高效地完成常见任务。本章将介绍一些重要的标准库,并通过实际应用来展示它们的用法。
8.1 字符串库
字符串库提供了处理字符串的各种函数。
local s = "Hello, Lua!"
-- 字符串长度
print(#s) -- 输出: 11
-- 字符串转换
print(string.upper(s)) -- 输出: HELLO, LUA!
print(string.lower(s)) -- 输出: hello, lua!
-- 子字符串
print(string.sub(s, 1, 5)) -- 输出: Hello
-- 查找和替换
print(string.find(s, "Lua")) -- 输出: 8 10
print(string.gsub(s, "Lua", "World")) -- 输出: Hello, World! 1
-- 字符串格式化
print(string.format("Pi: %.2f", math.pi)) -- 输出: Pi: 3.14
8.2 表处理库
表处理库提供了操作表的函数。
local t = {10, 20, 30, 40, 50}
-- 插入和删除
table.insert(t, 60)
table.remove(t, 1)
print(table.concat(t, ", ")) -- 输出: 20, 30, 40, 50, 60
-- 排序
table.sort(t)
print(table.concat(t, ", ")) -- 输出: 20, 30, 40, 50, 60
-- 最大值(Lua 5.2+)
print(table.maxn(t)) -- 输出: 5
8.3 数学库
数学库提供了基本的数学函数。
-- 基本运算
print(math.abs(-10)) -- 输出: 10
print(math.floor(3.7)) -- 输出: 3
print(math.ceil(3.7)) -- 输出: 4
-- 三角函数
print(math.sin(math.pi / 2)) -- 输出: 1.0
-- 随机数
math.randomseed(os.time())
print(math.random()) -- 输出: 0到1之间的随机数
print(math.random(1, 10)) -- 输出: 1到10之间的随机整数
8.4 输入输出库
IO库提供了文件操作和标准输入输出的功能。
-- 读取文件
local file = io.open("example.txt", "r")
if file then
local content = file:read("*all")
print(content)
file:close()
end
-- 写入文件
local file = io.open("output.txt", "w")
if file then
file:write("Hello, World!\n")
file:close()
end
-- 用户输入
print("Enter your name:")
local name = io.read()
print("Hello, " .. name)
8.5 操作系统库
OS库提供了与操作系统交互的函数。
-- 当前时间
print(os.date()) -- 输出当前日期和时间
-- 执行系统命令
os.execute("echo Hello from system")
-- 获取环境变量
print(os.getenv("PATH"))
-- 测量代码执行时间
local start = os.clock()
for i = 1, 1000000 do end
print(string.format("Time taken: %.2f seconds", os.clock() - start))
8.6 实例:配置文件解析器
让我们创建一个简单的配置文件解析器,它可以读取和写入配置文件。
local ConfigParser = {}
function ConfigParser.read(filename)
local config = {}
local file = io.open(filename, "r")
if not file then return config end
for line in file:lines() do
local key, value = line:match("^(%w+)%s*=%s*(.+)$")
if key and value then
config[key] = value
end
end
file:close()
return config
end
function ConfigParser.write(filename, config)
local file = io.open(filename, "w")
if not file then return false end
for key, value in pairs(config) do
file:write(string.format("%s = %s\n", key, value))
end
file:close()
return true
end
-- 使用示例
local config = ConfigParser.read("config.ini")
config.newSetting = "value"
ConfigParser.write("config.ini", config)
8.7 实例:简单的文本游戏
最后,让我们使用学到的知识创建一个简单的文本冒险游戏。
local game = {
player = {
name = "",
health = 100,
inventory = {}
},
locations = {
{name = "Forest", description = "A dark and mysterious forest."},
{name = "Cave", description = "A damp and echoing cave."},
{name = "Mountain", description = "A tall and snowy mountain."}
},
currentLocation = 1
}
function game.start()
print("Welcome to the Text Adventure!")
print("What's your name?")
game.player.name = io.read()
print("Hello, " .. game.player.name .. "! Your adventure begins.")
game.showLocation()
end
function game.showLocation()
local location = game.locations[game.currentLocation]
print("\nYou are in the " .. location.name)
print(location.description)
game.showOptions()
end
function game.showOptions()
print("\nWhat would you like to do?")
print("1. Move to next location")
print("2. Check inventory")
print("3. Quit")
local choice = tonumber(io.read())
if choice == 1 then
game.moveToNextLocation()
elseif choice == 2 then
game.checkInventory()
elseif choice == 3 then
print("Thanks for playing!")
os.exit()
else
print("Invalid choice. Try again.")
game.showOptions()
end
end
function game.moveToNextLocation()
game.currentLocation = game.currentLocation % #game.locations + 1
game.showLocation()
end
function game.checkInventory()
print("\nInventory:")
if #game.player.inventory == 0 then
print("Your inventory is empty.")
else
for _, item in ipairs(game.player.inventory) do
print("- " .. item)
end
end
game.showOptions()
end
-- 开始游戏
game.start()
练习
- 扩展配置文件解析器,使其支持注释和节(sections)。
- 为文本游戏添加更多功能,如战斗系统、物品收集和使用等。
- 创建一个使用 Lua 标准库的简单日志系统,支持不同的日志级别和输出格式。
通过本章的学习,你应该能够熟练使用 Lua 的标准库,并能够将这些知识应用到实际项目中。这些库和技能将大大提高你的 Lua 编程效率和能力。