function get_meminfo(fn)
local r={}
local f=assert(io.open(fn,"r"))
-- read the whole file into s
local s=f:read("*a")
-- now enumerate all occurances of "SomeName: SomeValue"
-- and assign the text of SomeName and SomeValue to k and v
for k,v in string.gmatch(s,"(%w+): *(%d+)") do
-- Save into table:
r[k]=v
end
f:close()
return r
end
-- use it
m=get_meminfo("/proc/meminfo")
print(m.MemTotal, m.MemFree)
for line in io.lines("/proc/meminfo") do
if line:find("MemTotal") then --// Syntactic sugar for string.find(line, "MemTotal")
--// If logic here...
elseif --// I don't quite understand this part in your code.
end
end
Пример с CSV файлом:
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
startData = 2
n = 1
local f = io.open("/tmp/dev/DEF-9xx.csv", "r")
for line in f:lines() do
if ( n >= startData ) then
print("--> "..n.." <--")
TBL = line:split(";");
for i = 1, #TBL do
print(i .. ") " .. TBL[i])
end
end
n = n + 1
end
f:close()
Результат выглядит так:
--> 1 <--
1) АВС/ DEF
2) От
3) До
4) Емкость
5) Оператор
6) Регион
7) ИНН
--> 2 <--
1) 900
2) 0000000
3) 0061999
4) 62000
5) ООО "Т2 Мобайл"
6) Краснодарский край
7) 7743895280
--> 3 <--
...
...
...
и т.д.