标签:
表格格式为
INT STRING INT INT INT INT INT Id Desc Type SceneId OpenId MaxPliesp ClearancePlies 0 第1关 0 1 -1 110 80 1 第2关 0 1 0 120 90 2 第3关 0 2 1 130 100
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) ~= 0 then for i=1,string.len(str) do new_str= string.sub(str,i,i) if (string.byte(new_str) >=48 and string.byte(new_str) <=57) or (string.byte(new_str)>=65 and string.byte(new_str)<=90) or (string.byte(new_str)>=97 and string.byte(new_str)<=122) 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=1,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[0]==‘#‘) then return true end return false end local TableFieldType = { INT = 0, FLOAT = 1, STRING = 2, UNKNOW = 3 } 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=1,#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=1,#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=1,#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=1,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(0) print(data.Desc)
标签:
原文地址:http://www.cnblogs.com/mrblue/p/5704399.html