From: Leslie Viljoen Date: 2010-10-25T23:50:25+09:00 Subject: Re: Utilizing data from a csv file On Sun, Oct 24, 2010 at 8:04 PM, Greg Barozzi wrote: > Paul Roche wrote in post #956704: >> Hi I basically want to create a function that takes in data that has >> been taken in from a csv file. >> > > Hi Paul, > > It looks like you have a handle on your data structure but need help > parsing the data from your csv file. I wrote a short program do handle > data from csv files a little while back. Maybe looking it over will help > you with your own project. > > Code: > > filename = 'data.csv' > grades = [] > heading = [] > values = [] > i = 0 > > > file = File.new(filename, 'r') > > file.each_line("\n") do |row| >  columns = row.split(",") Split is simple, but it doesn't deal with CSV rows which have strings with commas in them. Like this: apple, pear, "banana, salad", apricot The FasterCSV gem is an excellent way to read and write CSV files and it deals with these sorts of problems: http://fastercsv.rubyforge.org