lua - Lua - 将 .txt 项目列表转换为表格,然后迭代
问题描述
我有一个很长的系统生成代码列表(在 text.txt 文件中),我无法弄清楚如何将它们全部转换为合适的结构,然后我可以逐行迭代。
Power
0000 0070 0000 0032 0080 0040 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0030 0010 0010 0010 0030 0010 0030 0010 0030 0010 0030 0010 0010 0010 0010 0010 0030 0010 0010 0010 0030 0010 0030 0010 0010 0010 0030 0010 0030 0010 0030 0010 0ACD
Power$1
0000 006C 0022 0002 015B 00AD 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 05F7 015B 0057 0016 0E6C
Power$2
0000 0068 0000 0022 0169 00B4 0017 0044 0017 0044 0017 0017 0017 0017 0017 0017 0017 0044 0017 0017 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0044 0017 0017 0017 0044 0017 0017 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0017 0017 0017 0017 0044 0017 0017 0017 0044 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0017 0017 0636
Etc.
理想情况下,我需要实现以下顺序。
- 读取/记录命令的名称(将其写入日志/另一个 txt 文件),
- 传输相应的代码,(更新 log/txt 以显示它已被发送)
- 稍等片刻,
- 转到列表中的下一个。
解决方案
您可以使用以下功能解决此问题:
io.lines('text.txt')
:返回一个迭代器函数,允许您读取文件的每一行。
io.open('log.txt', 'w'):write(content)
: 允许您快速写入文件(由io.open
返回文件句柄和组成file:write
)。
io.flush()
:刷新流。如果流从 write() 中保存了任何字符,请立即将它们写入其预期目标(保存文件)。
string.find(pattern)
:使用提供的模式,返回找到的子字符串的位置。
string.len()
: 返回字符串的长度,在示例中我使用它来验证它不是空行。
table.concat(table, delimiter)
: 返回一个字符串,其中包含连接在一起的表的所有元素,由指定为第二个参数的分隔符分隔。
如果您确实需要暂停,请使用os.clock()
它返回程序使用的 CPU 时间的近似值(以秒为单位)。
像这样的东西应该适合你:
local logFile = io.open('log.txt', 'w')
local logs = {} -- We will use this to log the messages to log.txt later
local i = 1;for line in io.lines("text.txt") do
-- %d represents digits, %A represents all non-letter characters this translates to: "if the line contains "$x" or only contains letters"
if (line:find('$%d') or not line:find('%A')) and line:len() > 1 then
logs[#logs+ 1] = 'Command: ' .. line
elseif line:find('%d') then
-- This will run if the sequence does not contain $x or only contains letters (such as Power$1 or Power)
-- If you want to iterate through every single instruction, you can use %S+ to separate by whitespaces
local x = 1;for instruction in line:gmatch('%S+') do
print('Instruction ' .. x .. ' at line ' .. i, instruction)
x = x + 1
-- Wait 1 second (this allows you to read the current instruction but makes it slower)
local pause = os.clock()
repeat until os.clock() > pause + 1
end
-- If you want to write it during the loop use logFile:write(content); logFile:flush(),
-- otherwise append it to logs[#logs + 1] and write it when the program is finished using table.concat()
logs[#logs + 1] = 'Sequence: ' .. line
end; i = i + 1 -- Raise the line number
end
-- Concatenate the information and save it to logs file
logFile:write(table.concat(logs, '\n'))
logFile:flush()
这为 log.txt 生成了以下输出:
Command: Power
Sequence: 0000 0070 0000 0032 0080 0040 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0030 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0010 0030 0010 0010 0010 0030 0010 0010 0010 0030 0010 0030 0010 0030 0010 0030 0010 0010 0010 0010 0010 0030 0010 0010 0010 0030 0010 0030 0010 0010 0010 0030 0010 0030 0010 0030 0010 0ACD
Command: Power$1
Sequence: 0000 006C 0022 0002 015B 00AD 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041 0016 0016 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 05F7 015B 0057 0016 0E6C
Command: Power$2
Sequence: 0000 0068 0000 0022 0169 00B4 0017 0044 0017 0044 0017 0017 0017 0017 0017 0017 0017 0044 0017 0017 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0044 0017 0017 0017 0044 0017 0017 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0017 0017 0017 0017 0044 0017 0017 0017 0044 0017 0044 0017 0017 0017 0017 0017 0044 0017 0044 0017 0017 0017 0636
并将以下内容打印到控制台:
Instruction 1 at line 3 0000
Instruction 2 at line 3 0070
Instruction 3 at line 3 0000
Instruction 4 at line 3 0032
Instruction 5 at line 3 0080
Instruction 6 at line 3 0040
Instruction 7 at line 3 0010
Instruction 8 at line 3 0010
Instruction 9 at line 3 0010
. . .
推荐阅读
- css - 在本机反应中放置没有绝对位置的视图
- matlab - 如何在没有eig的matlab中计算特征向量
- python - 如何选择和替换列中的相似匹配项
- java - 如何在 Maven、Spring、Hibernate 项目中使用 Netbeans 在开发期间加速 Hibernate 初始化
- angular - 异步管道如何知道何时更新
- c# - .Net core 2.2 未将 302 的状态代码更新为 401。 OnRedirectToLogin 事件未触发
- python - 在python中检索MySQL时间戳
- javascript - 在画布中居中图像
- angular - 多选默认值
- python - Django:我的视图进入了 Try 部分,也进入了 except 部分?