# FasterCSV joining rows?

**URL:** <https://www.ruby-forum.com/t/fastercsv-joining-rows/152874>\
**Category:** Ruby\
**Created:** [November 8, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/fastercsv-joining-rows/152874 "2008-11-08T20:36:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![krisw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@krisw](https://www.ruby-forum.com/u/krisw)\
**Post date:** [November 8, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/fastercsv-joining-rows/152874/1 "2008-11-08T20:36:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krisw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/dc4da7/32.png) [@krisw](https://www.ruby-forum.com/u/krisw)\
**Post date:** [November 8, 2008, 10:47pm UTC](https://www.ruby-forum.com/t/fastercsv-joining-rows/152874/2 "2008-11-08T22:47:17Z")

</div>

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
