From: Greg Barozzi Date: 2010-10-25T03:04:54+09:00 Subject: Re: Utilizing data from a csv file 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(",") #p columns grades << columns end grades.each do |line| heading = line if i == 0 values = line if i == 1 if i > 2 # Print Unit title and student name print values[0], "\nName: ", line[0],",", line[1],"\n\n" # print total score and final grade print "Total Score: ", line[2], " -- ", line[3], "\n\n" # print breakdown HOURS print heading[4], ": ", line[5], " of ", values[4], "\n" # print breakdown PARTICIPATION print heading[5], ": ",line[6], "% of ", values[5], "\n" # print breakdown Assignment Points print heading[6], ": ",line[7], " of ", values[6], " total points\n" # print breakdown Assignments print heading[7], ": ",line[8], "% of ", values[7], "\n" # print breakdown WA Law print heading[8], ": ",line[9], "% of ", values[8], "\n" # print breakdown Quiz print heading[9], ": ",line[10], "% of ", values[9], "\n" # print breakdown Exam print heading[10], ": ",line[11], "% of ", values[10], "\n" # print breakdown EC print heading[11].chomp, ": ",line[12], "\n\n\n\n" #print line end i += 1 end END CODE It's a little ugly with the formating. I wasn't planning on showing this to anyone when I wrote it. It reads the csv file line by line and then based on the cell position it stores the data in an array. You should be able to substitute my arrays with your class objects no problem. Hope this helps you. Greg- -- Posted via http://www.ruby-forum.com/.