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 10print(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)) -- 输出: 10print(math.floor(3.7)) -- 输出: 3print(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 thenlocal content = file:read("*all")print(content)file:close()end-- 写入文件local file = io.open("output.txt", "w")if file thenfile: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 endprint(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 endfor line in file:lines() dolocal key, value = line:match("^(%w+)%s*=%s*(.+)$")if key and value thenconfig[key] = valueendendfile:close()return configendfunction ConfigParser.write(filename, config)local file = io.open(filename, "w")if not file then return false endfor key, value in pairs(config) dofile:write(string.format("%s = %s\n", key, value))endfile:close()return trueend-- 使用示例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()endfunction game.showLocation()local location = game.locations[game.currentLocation]print("\nYou are in the " .. location.name)print(location.description)game.showOptions()endfunction 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 thengame.moveToNextLocation()elseif choice == 2 thengame.checkInventory()elseif choice == 3 thenprint("Thanks for playing!")os.exit()elseprint("Invalid choice. Try again.")game.showOptions()endendfunction game.moveToNextLocation()game.currentLocation = game.currentLocation % #game.locations + 1game.showLocation()endfunction game.checkInventory()print("\nInventory:")if #game.player.inventory == 0 thenprint("Your inventory is empty.")elsefor _, item in ipairs(game.player.inventory) doprint("- " .. item)endendgame.showOptions()end-- 开始游戏game.start()
练习
- 扩展配置文件解析器,使其支持注释和节(sections)。
- 为文本游戏添加更多功能,如战斗系统、物品收集和使用等。
- 创建一个使用 Lua 标准库的简单日志系统,支持不同的日志级别和输出格式。
通过本章的学习,你应该能够熟练使用 Lua 的标准库,并能够将这些知识应用到实际项目中。这些库和技能将大大提高你的 Lua 编程效率和能力。
