【发布时间】:2014-01-02 03:26:30
【问题描述】:
我想做什么
我想做的很简单。我想使用 Lua 来检查 Plist 文件中的行。
假设如果 Plist 中的一行是 <integer>-1.00</integer>,我需要将 .00 剪掉以使其成为 <integer>-1</integer>。
我做了什么
我使用函数读取整个文件内容并逐行检查和替换。
local function doLineClean( cont )
newcont = ''
string.gsub( cont, "(.-)\r?\n", function(line)
if string.match( line, "<integer>.-<%/integer>" ) then
string.gsub( line, "<.->(.-)<.->", function(v)
a, b = string.find(v,"%..*")
if a and b then
v = string.sub( v, 0, a - 1 )
end
line = "\t\t<integer>"..v.."</integer>"
end )
end
newcont = newcont .. line .. '\n'
end )
return newcont
end
我的问题
有没有更高效、更优雅的方式来完成同样的工作?
【问题讨论】:
-
要清楚,您不仅希望
-1.00成为-1,还希望截断<integer>元素内的所有浮点数?如果是-1.99呢? -
@Phrogz 是的,截断所有浮点数,
-1.99应该是-1
标签: lua lua-patterns