From: "Jesús Gabriel y Galán" Date: 2011-10-06T16:11:32+09:00 Subject: Re: Read thru Csv file and store it in variables On Thu, Oct 6, 2011 at 1:24 AM, ideal one wrote: > "Jesús Gabriel y Galán" wrote in post > #1025230: >> On Wed, Oct 5, 2011 at 11:14 PM, ideal one >> 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. > > Hello, >     Thanks for your Info, Sorry if i am not clear with my requirement. > Let me clearly explain below: I am looking for DATA DRIVEN Scenario. > > I read CSV file and use that data in my code. ie If i have 10 rows, i > will run my program 10 times. > > example: > > > def testprog > type name > type account > type t1 > type t2 > type date > end > > > > > i will have to iterate each time into my code and use different row > values, its sort of Data Driven Automation. > let me know if my requirement makes sense. I'm not really sure. Something like this maybe (where type is something meaningful for you)? def my_prog values type values["name"] type values["account"] type values["t1"] type values["t2"] type values["date"] end CSV.foreach("data.txt", {:headers => true}) do |row| my_prog row.to_hash end Jesus.