表格格式为
INT STRING INT INT INT INT INT
Id Desc Type SceneId OpenId MaxPliesp ClearancePlies
第1关 -
第2关
第3关
1、先给出用到的字符串处理函数代码
--字符串分割函数
--传入字符串和分隔符,返回分割后的table
function string.split(str, delimiter)
if str==nil or str=='' or delimiter==nil then
return nil
end local result = {}
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match)
end
return result
end --字符串按位分割函数
--传入字符串,返回分割后的table,必须为字母、数字,否则返回nil
function string.gsplit(str)
local str_tb = {}
if string.len(str) ~= then
for i=,string.len(str) do
new_str= string.sub(str,i,i)
if (string.byte(new_str) >= and string.byte(new_str) <=) or (string.byte(new_str)>= and string.byte(new_str)<=) or (string.byte(new_str)>= and string.byte(new_str)<=) then
table.insert(str_tb,string.sub(str,i,i))
else
return nil
end
end
return str_tb
else
return nil
end
end function string.trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
2、表格对象代码(class见我上上篇文章lua高阶用法 OO的实现)
TableData = class() function TableData:ctor()
self.data = {}
end function TableData:GetById(id)
local num = #self.data
for i=,num do
if(id==self.data[i].Id) then
return self.data[i];
end
end
print( string.format("nil==TableData:GetById(%d)",id))
return nil;
end
3、给出读表器代码
require 'Utility/LuaStringExt'
require 'Table/TableData' TableReader = {} function TableReader.ReadAllLines(text)
return string.split(text,'\n')
end function TableReader.IsEmptyLine(str)
if(str=='' or str[]=='#') then
return true
end
return false
end local TableFieldType =
{
INT = ,
FLOAT = ,
STRING = ,
UNKNOW =
} local Syntax_INT = 'INT'
local Syntax_FLOAT = 'FLOAT'
local Syntax_STRING = 'STRING' function TableReader.ParseType(str) local types = {}
local fields = string.split(str,'\t')
for i=,#fields do
local sytax = fields[i] if(Syntax_INT==sytax) then
table.insert(types,TableFieldType.INT)
elseif(Syntax_FLOAT==sytax) then
table.insert(types,TableFieldType.FLOAT)
elseif(Syntax_STRING==sytax) then
table.insert(types,TableFieldType.STRING)
else
table.insert(types,TableFieldType.UNKNOW)
print('Error:',i,'unknow type['..sytax..']')
end
end
return types
end function TableReader.ParseField(str)
local fieldsTable = {}
local fields = string.split(str,'\t') for i=,#fields do
table.insert(fieldsTable,fields[i])
end
return fieldsTable
end function TableReader.ParseValue(fieldsTable,typeTable,str)
local ret = {}
local fields = string.split(str,'\t')
for i=,#fields do
if(TableFieldType.INT==typeTable[i] or TableFieldType.FLOAT==typeTable[i]) then
ret[fieldsTable[i]] = tonumber(fields[i])
elseif(TableFieldType.STRING==typeTable[i]) then
ret[fieldsTable[i]] = fields[i]
else
print('Error:ParseValue',i)
end
end
return ret
end function TableReader.LoadTable(text)
local tab = TableData.new(); local lines = TableReader.ReadAllLines(text)
local lineNum = #lines local types = nil
local fieldsTable = nil for i=,lineNum do
local str = string.trim(lines[i]) if(not TableReader.IsEmptyLine(str)) then if(nil==types) then
types = TableReader.ParseType(str)
elseif(nil==fieldsTable) then
fieldsTable = TableReader.ParseField(str)
else
table.insert(tab.data,TableReader.ParseValue(fieldsTable,types,str))
end end
end return tab
end
4、使用代码
local table = TableReader.LoadTable(str)
local data = table:GetById()
print(data.Desc)