On Wed, Oct 5, 2011 at 11:14 PM, ideal one [email protected]
wrote:
I am not aware of this error, Can any of you plz recommend better way to
Run Program
csv_data = CSV.read(path)
headers = csv_data.shift.map {|i| i.to_s }
data1 = csv_data.map {|row| row.map {|cell| cell.to_s } }
hashe1 = string_data.map {|row| Hash[*headers.zip(row).flatten] }
puts “Done”
I don’t really understand what do you want. You want to create a hash
in each iteration with the keys being the headers and the values being
the value in each row?
For example:
irb(main):003:0> CSV.foreach(“data.txt”, {:headers => true}) do |row|
irb(main):004:1* p row.to_hash
irb(main):005:1> end
{“name”=>“test1”, “account”=>“1”, “date”=>“10-Sep”, “t1”=>“QA1”,
“t2”=>“PQ1”}
{“name”=>“test2”, “account”=>“2”, “date”=>“11-Sep”, “t1”=>“QA2”,
“t2”=>“PQ2”}
{“name”=>“test3”, “account”=>“3”, “date”=>“12-Sep”, “t1”=>“QA3”,
“t2”=>“PQ3”}
=> nil
This is my data.txt:
name,account,t1,t2,date
test1,1,QA1,PQ1,10-Sep
test2,2,QA2,PQ2,11-Sep
test3,3,QA3,PQ3,12-Sep
Maybe you can use the values directly, but if you need a hash that’s
how I’d do it.
Hope this helps,
Jesus.