# FasterCsv writing columns

**URL:** <https://rubytalk.org/t/fastercsv-writing-columns/50510>\
**Category:** ruby-talk\
**Created:** [3 December 2008 17:15 UTC](https://rubytalk.org/t/fastercsv-writing-columns/50510 "2008-12-03T17:15:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mordechai\_Mr](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Mordechai\_Mr](https://rubytalk.org/u/Mordechai_Mr)\
**Post date:** [3 December 2008 17:15 UTC](https://rubytalk.org/t/fastercsv-writing-columns/50510/1 "2008-12-03T17:15:41Z")

</div>

Hi,

Can someone help me with FCSV writing ?

If I have an array of integers lets say  
someList[0].listName = "header1"  
someList[0].someArray = [2, 4, 8, 9]

someList[1].listName = "header2"  
someList[1].someArray = [10, 11, 12, 13]

I'm desperately trying to figure out how to use FasterCSV to produce a  
csv file that will show:

header1, header2  
2, 10,  
4, 11,  
8, 12,  
9, 13,  
header1\_avg,header2\_avg

I've read through the FasterCSV documents, and I just can't figure  
things out. I know I most likely need to use the FCSV table, or read  
the array in and use the FCSV.parse...

Thanks for your help.  
mord

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [3 December 2008 20:07 UTC](https://rubytalk.org/t/fastercsv-writing-columns/50510/2 "2008-12-03T20:07:33Z")

</div>

> Hi,

Hello.

> Can someone help me with FCSV writing ?

I'll sure try.

> If I have an array of integers lets say  
> someList[0].listName = "header1"  
> someList[0].someArray = [2, 4, 8, 9]
> 
> someList[1].listName = "header2"  
> someList[1].someArray = [10, 11, 12, 13]
> 
> I'm desperately trying to figure out how to use FasterCSV to produce a  
> csv file that will show:
> 
> header1, header2  
> 2, 10,  
> 4, 11,  
> 8, 12,  
> 9, 13,  
> header1\_avg,header2\_avg

I would use code like:

#!/usr/bin/env ruby -wKU

require "rubygems"  
require "faster\_csv"

Column = Struct.new(:header, :data) do  
&nbsp;&nbsp;&nbsp;def average  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0 if data.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.inject(0) { |sum, n| sum + n } / data.size  
&nbsp;&nbsp;&nbsp;end  
end  
columns = [Column.new("header1", [2, 4, 8, 9]),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Column.new("header2", [10, 11, 12, 13]) ]

FCSV do |csv|  
&nbsp;&nbsp;&nbsp;csv \<\< columns.map { |c| c.header }  
&nbsp;&nbsp;&nbsp;columns.map { |c| c.data.size }.max.times do |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;csv \<\< columns.map { |c| c.data[i] }  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;csv \<\< columns.map { |c| c.average }  
end

\_\_END\_\_

That spits the data to STDOUT. If you would rather send it to a file, just change this line:

FCSV do |csv|

to:

FCSV.open("my\_file\_name.csv", "w") do |csv|

> I've read through the FasterCSV documents, and I just can't figure  
> things out. I know I most likely need to use the FCSV table, or read  
> the array in and use the FCSV.parse...

Na. These methods are for reading CSV data, not writing it.

Hope that helps.

James Edward Gray II

> **···**
>
> On Dec 3, 2008, at 11:15 AM, Mordechai Mr wrote:
