Try...

CSV.filter(input,output,:col_sep => ",",:headers => true,
:return_headers => true) do |csv|

Abinoam Jr.

On Sat, Feb 15, 2014 at 8:02 AM, Arup Rakshit <lists / ruby-forum.com> wrote:
> I fixed my code :
>
> require 'csv'
>
> content = <<CSV
> id,first name,last name,age,sex
> 12,arup,rakshit,26,M
> 11,ayan,das,25,M
> 44,puja,roy,19,F
> 18,Dolly,Sen,21,F
> CSV
>
> File.write('a.csv',content)
>
> #CSV.foreach('a.csv') do |row|
> #  p row
> #end
>
> input = File.new('a.csv','r')
> output = File.new('b.csv','w')
>
> CSV.filter(input,output,:col_sep => ",",:headers => true) do |csv|
>   csv['status'] = "eligible" if csv['sex'] == "F"
> end
> output.close
>
> CSV.foreach('b.csv') do |row|
>   p row
> end
>
> # >> ["12", "arup", "rakshit", "26", "M"]
> # >> ["11", "ayan", "das", "25", "M"]
> # >> ["44", "puja", "roy", "19", "F", "eligible"]
> # >> ["18", "Dolly", "Sen", "21", "F", "eligible"]
>
> Question : why ['id','first name','last name','age','sex'] is missing
> from the output and how to add it ?
>
> --
> Posted via http://www.ruby-forum.com/.