From: "Jesús Gabriel y Galán" Date: 2011-10-06T06:33:02+09:00 Subject: Re: Read thru Csv file and store it in variables On Wed, Oct 5, 2011 at 11:14 PM, ideal one wrote: > "Jesús Gabriel y Galán" wrote in post > #1025175: >> On Wed, Oct 5, 2011 at 7:25 PM, ideal one wrote: >>> >>> Files/Ruby/lib/ruby/gems/1.9.1/gems/fastercsv-1.5.4/lib/faster_csv.rb:13:in >>> `const_missing': Please switch to Ruby 1.9's standard CSV library. It's >>> FasterCSV plus support for Ruby 1.9's m17n encoding engine. >>> (NotImplementedError) >>> >>> >>> I am not aware of this error, Can any of you plz recommend better way to >>> achieve the result. >> >> In Ruby 1.9, fasterCSV has been made the implementation of the stdlib >> CSV, so you should be able to remove the rubygems (which btw, is also >> included by default in 1.9) and just make require 'csv' and CSV >> instead of FasterCSV. >> >> http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html >> >> Jesus. > > Hi there, >           I did implement the csv using hash, but Is there a way i can > get data for each iteration from Hash. > > My Input > --------- > Name,Account,T1,T2,Date > test1,1,QA1,PQ1,10-Sep > test2,2,QA2,PQ2,11-Sep > test3,3,QA3,PQ3,12-Sep > > eg: In my Iteration, I want to utilize each > > 1st row: > Run Program > Name=test1 > Account=1 > T1=QA1 > end > > row2 values > Run Program > Name=test2 > Account=2 > T1=QA2 > end > > so on, till the end of the file. > > > require 'csv' > path = "C:/Users/test/Desktop/data.txt" > > 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.