FasterCSV joining rows?

K

Kris Windham

I was wondering if anyone could point me in the right direction for
solving the following problem:

I am appending to a csv every time a database change is made in a Rails
app I am working on.
I would like to join the rows together based on the first field.
If this field has duplicates, I would like to combine all values in each
row into one row.
Each row will have only one updated field.
All other fields will be denoted with an asterisk.
The following is an example:

100000000,[email protected],*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,888-888-8888,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,My Company was
changed,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,303 changed work
address,*,*,*,*,*,*,*,*,*,*

I need the above to look like the following:

100000000,[email protected],*,888-888-8888,*,*,*,*,*,*,*,*,*,303
changed work address,*,*,*,*,My Company was changed,*,*,*,*,*

Any help would be appreciated.

Thanks,

Kris Windham
 
J

James Gray

I was wondering if anyone could point me in the right direction for
solving the following problem:

I'll try.
I am appending to a csv every time a database change is made in a
Rails
app I am working on.
I would like to join the rows together based on the first field.
If this field has duplicates, I would like to combine all values in
each
row into one row.
Each row will have only one updated field.
All other fields will be denoted with an asterisk.
The following is an example:

100000000
,[email protected],*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,888-888-8888,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,My Company was
changed,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,303 changed work
address,*,*,*,*,*,*,*,*,*,*

I need the above to look like the following:

100000000,[email protected],*,888-888-8888,*,*,*,*,*,*,*,*,*,303
changed work address,*,*,*,*,My Company was changed,*,*,*,*,*

Any help would be appreciated.

Does code like the following give you any ideas?

#!/usr/bin/env ruby -wKU

require "rubygems"
require "faster_csv"

data = <<END_DATA
100000000
,[email protected],*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,888-888-8888,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,My Company was
changed,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,303 changed work
address,*,*,*,*,*,*,*,*,*,*
END_DATA

# rebuild records from changelog
records = Hash.new
FCSV.parse(data, :converters => lambda { |f| f == "*" ? nil : f }) do |
row|
key = row.shift
old = Array(records[key])
fields = [old, row].map { |r| r.size }.max
records[key] = (0...fields).map { |i| row || old }
end

# write records
FCSV do |csv|
records.each do |key, record|
csv << [key, *record].map { |f| f || "*" }
end
end
# >> 100000000,[email protected],*,888-888-8888,*,*,*,*,*,*,*,*,
303 changed work address,*,*,*,*,My Company was changed,*,*,*,*,*

__END__

Hope that helps.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top