Lua实现正序和倒序的文件读取方法

发布时间:2022-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Lua实现正序和倒序的文件读取方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
--table 特性
-- 使用table生成正序和倒序的链表

-- 使用table生成链表

list = nil
local file = io.oPEn("table.lua","r") -->打开本本件

PRe = nil
--将本文件按行顺序读入list中
for line in file:lines() do  
	current = {next = nil,value = line}
	pre = pre or current
	list = list or pre
	pre.next = current
	pre = current
end

file:close() -- 关闭文件

-- 输出list
local l = list
while l do
	print(l.value)
	l = l.next
end

-- 以下是按行倒序的方法
print("以下是按行倒序输出文件:\n")
local file = io.open("table.lua","r") -->打开本本件

list = nil --清空list之前的内容

for line in file:lines() do
	list = {next = list,value = line}
end

file:close() -- 关闭文件
-- 输出list
local l = list
while l do
	print(l.value)
	l = l.next
end

脚本宝典总结

以上是脚本宝典为你收集整理的Lua实现正序和倒序的文件读取方法全部内容,希望文章能够帮你解决Lua实现正序和倒序的文件读取方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:Lua倒序