我在Lua中有以下代码:
function getSystemLoad()
local file = io.popen('./check_cpu.pl')
local value = file:read('*l')
file:close()
return value
end
此功能的输出可能是: CPU OK:0.51% 或 CPU警告:76.5%
我需要此功能只返回十进制数,在这些情况下 0.51 或 76.5
答案 0 :(得分:1)
local function extractDecimalFrom( str )
local t = {}
for i in string.gmatch( str, "[0-9]+%.[0-9]+" ) do
t[#t+1] = tonumber(i)
end
return t
end
local s = "CPU OK: 0.51% or CPU WARNING: 76.5%"
local extractedDecimals = extractDecimalFrom( s )
for i=1, #extractedDecimals do
print(extractedDecimals[i])
end