views:

51

answers:

2

I have a CSV file which I'm parsing and inserting bits of it in a database. I'd like to write a parser which takes account of the fact that the column order may change in the future.

I thought that I could do this by grabbing the header line as an array, and for each line, putting the value in a dynamically created local variable (using eval). However this doesn't seem to work, as local variables don't seem to be accessible outside the eval. I've read elsewhere that this may be correct in ruby 1.9, but I'm using 1.8.7

Code such as:

headers = ["a", "b"]
  headers.each do |h|
  p e_str = h+"=1"
  eval(e_str)
end
puts a

simply doesn't work, giving

test.rb:6: undefined local variable or method `a' for main:Object (NameError)

though line 3 prints "a=1" and "b=1" as expected

Does anyone know how I could go about this?

+1  A: 

You can get a similar effect using an openstruct

require 'ostruct'

x = OpenStruct.new

headers = ["a", "b"]
headers.each do |h|
  x.send("#{h}=", 1)
end
puts x.a
puts x.b
Martin DeMello
+2  A: 

Given data.csv

"foo","bar","baz"
1,2,3
4,5,6

then

#!/usr/bin/ruby1.8

require 'csv'

rows = CSV.readlines('data.csv')
header = rows[0]
data = rows[1..-1].map do |row|
  {}.tap do |h|
    header.each.with_index.map do |name, i|
      h[name] = row[i]
    end
  end
end

p data
# => [{"baz"=>"3", "foo"=>"1", "bar"=>"2"}, {"baz"=>"6", "foo"=>"4", "bar"=>"5"}]

Note: Require 1.8.7 or later.

Wayne Conrad
As @wayne-conrad says, it's way better to just use the standard library's csv parser. See more here: http://ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html
Tim Snowhite