From: Robert Klemme Date: 2007-09-13T03:15:18+09:00 Subject: Re: CVS parsing, counting rows and row items On 12.09.2007 16:05, Max Russell wrote: > I have some CSV data which looks like the following (it is the output > from the RMTrack defect management tool): > > Column titles: > Issue # Date & Time Opened Summary Created by User Assigned To > Resolution Date & Time Closed > > example data: > 1074 16/05/2006 Something is broken import bob Ignore 26/03/2007 > 1807 17/07/2006 Another thing doesn't work rsmith hmaguire Ignore > 27/03/2007 > > Basically, I'm finding the CSV documentation > http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html > of very little help... > > What is the best way of going about parsing the data to get the row > count? > > From that I feel I could work out the unique item counts I'm looking > for. > > I was thinking about something like this... > > rowcount = 0 > CSV::Reader.parse(filehandle) do |row| > rowcount =+ 1 > return rowcount > end > > but I'm getting tangled up here. You have the return statement in the wrong place. If you just want to count lines in a file then you can just do "wc -l ". If you want to do it in Ruby you can do "ruby -ne 'END{puts $.}' ". If you want to do it inside a script, an efficient variant is this: count=File.open(f){|io| c=0;io.each { c+=1 }; c} Kind regards robert