[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

CVS parsing, counting rows and row items

Max Russell

9/12/2007 2:06:00 PM

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/...
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.
--
Posted via http://www.ruby-....

2 Answers

William James

9/12/2007 3:10:00 PM

0

On Sep 12, 9:05 am, Max Russell <thedoss...@gmail.com> 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

CSV stands for comma-separated values. Where are the commas?

To get the number of lines in a file:

IO.readlines('my_file').size

Robert Klemme

9/12/2007 6:10:00 PM

0

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/...
> 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 <file>". If you want
to do it in Ruby you can do "ruby -ne 'END{puts $.}' <file>". 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