Old 10-07-2019, 10:20 AM   #1
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 742
Default Example of how to use this CSV function (need i.o. help)

HI gang,

I know there are a ton of CSV file examples out there, but I'm really clueless about the file IO code and path syntax etc.

Can anyone here post or point to a "Load CSV" function that will work with the parsing code below? My CSV file might look like this


CSV example:

Vox_1, 23098492, 43898432
Vox_2, 34895958, 56985904
.
.
.etc.

Code taken from here: http://lua-users.org/wiki/LuaCsv
Code:
function ParseCSVLine (line,sep) 
	local res = {}
	local pos = 1
	sep = sep or ','
	while true do 
		local c = string.sub(line,pos,pos)
		if (c == "") then break end
		if (c == '"') then
			-- quoted value (ignore separator within)
			local txt = ""
			repeat
				local startp,endp = string.find(line,'^%b""',pos)
				txt = txt..string.sub(line,startp+1,endp-1)
				pos = endp + 1
				c = string.sub(line,pos,pos) 
				if (c == '"') then txt = txt..'"' end 
				-- check first char AFTER quoted string, if it is another
				-- quoted string without separator, then append it
				-- this is the way to "escape" the quote char in a quote. example:
				--   value1,"blub""blip""boing",value3  will result in blub"blip"boing  for the middle
			until (c ~= '"')
			table.insert(res,txt)
			assert(c == sep or c == "")
			pos = pos + 1
		else	
			-- no quotes used, just look for the first separator
			local startp,endp = string.find(line,sep,pos)
			if (startp) then 
				table.insert(res,string.sub(line,pos,startp-1))
				pos = endp + 1
			else
				-- no separator found -> use rest of string and terminate
				table.insert(res,string.sub(line,pos))
				break
			end 
		end
	end
	return res
end
__________________
Cheers,
Andrew K
v5.982/64 Mac 10.12.+, i7 Quad 2.9GHz, 24GB
Thonex is offline   Reply With Quote
Old 10-07-2019, 10:30 AM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,238
Default

That snippet is really only necessary if you need to have commas in the values, i.e. value a, value b, "value c is longer, see?", value d.

I can't test this right now, but try:
Code:
-- CSVs will often have their keys as the first line, in which 
-- case you'd have to grab them while reading the file
local csvKeys = {'name', 'someNumber', 'otherNumber'}
local entries = {}

local file, err = io.open("some/file.path", "r")

-- If we had an error, pop up the message and then abort
if err then
  reaper.MB(err, "Whoops!", 0)
  return
end

for line in file:lines() do
  local entry = {}
  local i = 1

  for val in line:gmatch("[^,]+") do -- matches anything that isn't a comma
    entry[csvKeys[i]] = val
    i = i + 1
  end
  
  entries[#entries + 1] = entry
end
Lokasenna is offline   Reply With Quote
Old 10-07-2019, 10:43 AM   #3
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 742
Default

Quote:
Originally Posted by Lokasenna View Post
That snippet is really only necessary if you need to have commas in the values, i.e. value a, value b, "value c is longer, see?", value d.

I can't test this right now, but try:
Code:
-- CSVs will often have their keys as the first line, in which 
-- case you'd have to grab them while reading the file
local csvKeys = {'name', 'someNumber', 'otherNumber'}
local entries = {}

local file, err = io.open("some/file.path", "r")

-- If we had an error, pop up the message and then abort
if err then
  reaper.MB(err, "Whoops!", 0)
  return
end

for line in file:lines() do
  local entry = {}
  local i = 1

  for val in line:gmatch("[^,]+") do -- matches anything that isn't a comma
    entry[csvKeys[i]] = val
    i = i + 1
  end
  
  entries[#entries + 1] = entry
end
Jesus... that was fast!! LOL

Thanks Loka... I'll try it now...
__________________
Cheers,
Andrew K
v5.982/64 Mac 10.12.+, i7 Quad 2.9GHz, 24GB
Thonex is offline   Reply With Quote
Old 10-07-2019, 11:40 AM   #4
Thonex
Human being with feelings
 
Join Date: May 2018
Location: Los Angeles
Posts: 742
Default

Wow... ok... so it works!

I added a function to help find the path if I keep the CSV in the Scripts folder.

This is so much easier than the CSV example on the Lua site.

updated code:

Code:
-- CSVs will often have their keys as the first line, in which 
-- case you'd have to grab them while reading the file

function Msg (param)
    reaper.ShowConsoleMsg(tostring (param).."\n")
end

function script_path()
    local str = debug.getinfo(2, "S").source:sub(2)
    return str:match("(.*/)")
end

function LOAD_CSV ()
    reaper.ClearConsole ()
    
    CSV_file_name = "Samples Loop Regions.csv"

    local csvKeys = {'name', 'n1', 'n2'}
    local entries = {}
    
    local file, err = io.open(script_path().. CSV_file_name, "r")
    
    -- If we had an error, pop up the message and then abort
    if err then
      reaper.MB(err, "Whoops!", 0)
      return
    end
    
    for line in file:lines() do
      local entry = {}
      local i = 1
    
      for val in line:gmatch("[^,]+") do -- matches anything that isn't a comma
        entry[csvKeys[i]] = val
        i = i + 1
        
      end
      
      entries[#entries + 1] = entry
    end
    --Msg(entries[3].n2)
end

LOAD_CSV()
__________________
Cheers,
Andrew K
v5.982/64 Mac 10.12.+, i7 Quad 2.9GHz, 24GB
Thonex is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.