我是lua的新手。实际上这个脚本用于游戏服务器,但我需要帮助从我的商店.txt文件中获取值。所以我的商店系统是每个玩家将保存在一个文件中。
我的文本文件
中的语法数据[name]:[points]:[which var point earned on index]
我的示例数据(此var点实际上是来自lua文件的写入数据,发送到文本文件)
jack:60:4
ronald:40:1
jack:30:1
所以当我从上面得到的是
列表local player = {{(name),(points),(which var point earned on index)},
{(name),(points),(which var point earned on index)},...};
我自己的代码
local line_data = {}
local point_file = io.open("points.txt", "r")
for line in point_file:lines() do
local playername, playerpoint, playervarpointname = line:match("(%d+):(%d+):(%d+)")
local player_data = {playername, playerpoint, playervarpointname}
for i = 1, #line do
player_data = [#player_data + 1] = line[i];
end
line_data[#line_data + 1] = player_data
end
这是正确的代码吗?
答案 0 :(得分:1)
local line_data = {}
local point_file = io.open("points.txt", "r")
for line in point_file:lines() do
local playername, playerpoint, playervarpointname = line:match("([^:]+):(%d+):(%d+)")
local player_data = {playername, playerpoint, playervarpointname}
table.insert(line_data, player_data)
end
或者,imho,'[a-zA-Z0-9] +',可以代替'[^:] +'