Forum: Ruby FasterCSV joining rows?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kris W. (Guest)
on 2008-11-08 21:36
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,removed_email_address@domain.invalid,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,888-888-8888,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,My Company was
changed,*,*,*,*,*
100000000,*,*,*,*,*,*,*,*,*,*,*,303 changed work
address,*,*,*,*,*,*,*,*,*,*

I need the above to look like the following:

100000000,removed_email_address@domain.invalid,*,888-888-8888,*,*,*,*,*,*,*,*,*,303
changed work address,*,*,*,*,My Company was changed,*,*,*,*,*

Any help would be appreciated.

Thanks,

Kris W.
James G. (Guest)
on 2008-11-08 23:47
(Received via mailing list)
On Nov 8, 2008, at 1:34 PM, Kris W. wrote:

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

I'll try.

>
> 100000000,removed_email_address@domain.invalid,*,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
,removed_email_address@domain.invalid,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*
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[i] || old[i] }
end

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

__END__

Hope that helps.

James Edward G. II
This topic is locked and can not be replied to.