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.

理想情况下,我需要实现以下顺序。

  1. 读取/记录命令的名称(将其写入日志/另一个txt文件(
  2. 传输相应的代码,(更新log.txt以显示已发送(
  3. 等待时间短
  4. 转到列表中的下一个

您可以使用以下函数来计算:

io.lines('text.txt'):返回一个迭代器函数,该函数允许您读取文件的每一行。

io.open('log.txt', 'w'):write(content):允许您快速写入文件(由返回文件句柄的io.openfile: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
. . .

非常感谢您的帮助,我使用了提供的示例并根据我的具体需求进行了定制。。

local lfs = require "lfs"
local read_file_path = "/mnt/nas/vera/text1.txt" -- # path to your file here
local write_file_path = "/mnt/nas/vera/text2.txt" -- # path to your file here
local pattern1 = "^.*Power.*$" -- # your pattern to find
local pattern2 = "^.*0000.*$" -- # your pattern to find
local logFile = io.open(write_file_path, 'w')
local logs = {} -- We will use this to log the messages to log.txt later 
local i = 1;for line in io.lines(read_file_path) do
if (line:find(pattern1) or not line:find('%A')) and line:len() > 1 then
local powerref = line
logs[#logs+ 1] = 'Command: ' .. powerref
elseif line:find(pattern2) then
local ircode = line
local x = 1 do
print('Command ' .. x .. ' is "' .. ircode .. '" found on line ' .. i)
x = x + 1
local pause = os.clock()
repeat until os.clock() > pause + 1
end
logs[#logs + 1] = 'Sequence: ' .. line
end; i = i + 1 -- Raise the line number
end
logFile:write(table.concat(logs, 'n'))
logFile:flush()

最新更新