Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
problem using CSV files — Gideros Forum

problem using CSV files

Hello gideros people,

Could I have some help please?

I am using @hgy29 CSV function he wrote a while ago. It is working fine for one csv file but not for two csv files.

The CSV function is:
function loadCSV(file)
 
	local function csvsplit(line)
		local t, w, l2 = {}, nil, nil
 
		while #line > 0 do
			w, l2 = line:match("\"([^\"]*)\"[,\r\n]?(.*)") -- Check for quoted string
			if not w then w, line = line:match("([^,]*)[,\r\n]?(.*)") -- Non quoted 
			else line = l2 end
			if not w then break end --Nothing or issue, break
 
			t[#t + 1] = w
		end
 
		return t
	end
 
	local header, csv = nil, {}
 
	for line in io.lines(file) do
		f = csvsplit(line)
		if not headers then -- Assume first line is headers
			headers = {} for n, v in ipairs(f) do headers[v] = n end
		else
			csv[#csv + 1] = f
		end
	end
 
	csv.getField = function(self, row, field) return self[row][headers[field]] end
 
	return csv
 
end
--csv=loadCSV("135-bis-bt.csv")
--print(csv:getField(4,"Value"))
--print(csv:getField(7,"Time")
In my case I have two csv files:
hijama _desc.csv
hijama.csv

When I use this code:
-- csv file 1
local csv_desc = loadCSV("fonts/hijama _desc.csv")
local mycsvtext1 = csv_desc:getField(3, "txtFr1")
print(mycsvtext1)
 
-- csv file 2 only works when csv file 1 is commented
local csv_hijama = loadCSV("fonts/hijama.csv")
local mycsvtxt2 = csv_hijama:getField(3, "front")
print(mycsvtxt2)
the result in the console is:

2) JOURS ET MOMENTS CONSEILLES
nil


But when I comment the first part (csv file 1) then the result is correct:
abc


I cannot have both csv files working at the same time to have the result:

2) JOURS ET MOMENTS CONSEILLES
abc


Do you know why is that?

Any help would be much appreciated. Thank you in advance for your time.
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

  • a sample code example demonstrating the problem.
    zip
    zip
    xgideroshelp01.zip
    157K
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • piepie Member
    Accepted Answer
    Hi :) there is an "s" missing at the end of header in loadCSV function, it should be like this:
     
    function loadCSV(file)
     
    	local function csvsplit(line)
    		local t, w, l2 = {}, nil, nil
     
    		while #line > 0 do
    			w, l2 = line:match("\"([^\"]*)\"[,\r\n]?(.*)") -- Check for quoted string
    			if not w then w, line = line:match("([^,]*)[,\r\n]?(.*)") -- Non quoted 
    			else line = l2 end
    			if not w then break end --Nothing or issue, break
     
    			t[#t + 1] = w
    		end
     
    		return t
    	end
     
    	local headers, csv = nil, {}  --here the s was missing
     
    	for line in io.lines(file) do
    		f = csvsplit(line)
    		if not headers then -- Assume first line is headers
    			headers = {} for n, v in ipairs(f) do headers[v] = n end
    		else
    			csv[#csv + 1] = f
    		end
    	end
     
    	csv.getField = function(self, row, field) return self[row][headers[field]] end
     
    	return csv
     
    end

    Likes: MoKaLux, hgy29

    +1 -1 (+2 / -0 )Share on Facebook
  • MoKaLuxMoKaLux Member
    edited June 2019
    @pie wonderful you nailed it straight! that is working now!

    super cool, thank you very much for your time @pie. You are my hero of the day!


    Likes: pie, antix

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.