Game Development Reference
In-Depth Information
until c ~= '"' -- quote not followed by quote?
if not i then error('unmatched "') end
local f=string.sub(s, fieldstart+1, i-1)
table.insert(t, (string.gsub(f, '""', '"')))
fieldstart=string.find(s, ',', i)+1
else -- unquoted; find next comma
local nexti=string.find(s, ',', fieldstart)
table.insert(t, string.sub(s, fieldstart, nexti-1))
fieldstart=nexti+1
end
until fieldstart>string.len(s)
return t
local s=""
for _,p in pairs(tt) do
s=s .. "," .. escapeCSV(p)
end
return string.sub(s, 2) -- Remove first comma
end
Escaping the CSV
Escaping in this context is not the same as avoiding , but instead means replacing the characters or
altering them to be used as a string. Here's an example of escaping CSV-formatted data:
-- Used to escape "'s by toCSV
function escapeCSV (s)
if string.find(s, '[,"]') then
s='"' .. string.gsub(s, '"', '""') .. '"'
end
return s
end
Formatting a Number with the Thousand Separator
While displaying scores, you might want to display them in a readable format. You can use the thousand
separator for this, like so:
function comma_value(amount)
local formatted=amount
while true do
 
Search WWH ::




Custom Search