On 6-6-2010 1:35, Jaroslav Hajtmar wrote:
The second is more universal, but it only works in ConTeXt MKIV. LuaLaTeX report error: "attempt to call field 'split' (a nil value) ... ."
function ParseCSVdata(string2parse, separator, leftdelimiter, rightdelimiter) Sep = (Sep == nil) and UserCSVSeparator or Sep Ld = (Ld == nil) and UserCSVLeftDelimiter or Ld Rd = (Rd == nil) and UserCSVRightDelimiter or Rd local separator = (separator == nil) and Sep or separator local leftdelimiter = (leftdelimiter == nil) and Ld or leftdelimiter local rightdelimiter = (rightdelimiter == nil) and Rd or rightdelimiter local result={} if leftdelimiter ~= '' and rightdelimiter ~= '' then string.gsub(string2parse, leftdelimiter.."(.-)"..rightdelimiter, function(a) table.insert(result,a) end ) else result=string.split(string2parse,separator) end return result end
You probably mean something ... UserCSVSeparator = ';' UserCSVLeftDelimiter = '' UserCSVRightDelimiter = '' function ParseCSVdata(string2parse, separator, leftdelimiter, rightdelimiter) leftdelimiter = leftdelimiter or Ld or UserCSVLeftDelimiter or "" rightdelimiter = rightdelimiter or Rd or UserCSVRightDelimiter or "" if leftdelimiter ~= '' and rightdelimiter ~= '' then local result = { } local pattern = string.esc(leftdelimiter) .. "(.-)" .. string.esc(rightdelimiter) for s in string.gmatch(string2parse,pattern) do table.insert(result,s) end return result else return string.split(string2parse,separator or Sep or UserCSVSeparator or ";") end end but anyhow, this might (name) clash at some point i guess, for instance because of using globals like Ld In order to be more or less compatible with the context module and third party code setup, in context you need to do: thirddata = thirddata or { } thirddata.cvs = { separator = ';', leftdelimiter = '', rightdelimiter = '', } function thirddata.cvs.parse(string2parse,separator,leftdelimiter,rightdelimiter) leftdelimiter = leftdelimiter or thirddata.leftdelimiter or "" rightdelimiter = rightdelimiter or thirddata.rightdelimiter or "" if leftdelimiter ~= "" and rightdelimiter ~= "" then local result = { } local pattern = string.esc(leftdelimiter) .. "(.-)" .. string.esc(rightdelimiter) for s in string.gmatch(string2parse,pattern) do table.insert(result,s) end return result else return string.split(string2parse,separator or thirddata.separator or ";") end end -- print(table.serialize(thirddata.cvs.parse("11,ss,cc",","))) -- print(table.serialize(thirddata.cvs.parse("[11] [ss] [cc]","","[","]"))) but i have no clue how this is used so there might be better approaches Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com | www.pragma-pod.nl -----------------------------------------------------------------