Hit post by accident! The full final example:
header_row = "year,month,day,price\n"
index_of = header_row.chomp.split(’,’).to_h.invert
=> {“month”=>1, “price”=>3, “day”=>2, “year”=>0}
data_row = "2002,1,17,1.5\n"
cells = data_row.chomp.split(’,’)
cells[index_of[‘year’]]
=> “2002”
Cheers,
Tom
Hit post by accident! The full final example:
header_row = “year,month,day,price\n”
index_of = header_row.chomp.split(‘,’).to_h.invert
=>> {“month”=>1, “price”=>3, “day”=>2, “year”=>0}
data_row = “2002,1,17,1.5\n”
cells = data_row.chomp.split(‘,’)
cells[index_of[‘year’]]
=>> “2002”
Isn’t there a CSV processing library available? I imagine an API
would provide hash-like access:
data = CSV.new(filename)
data.each do |record|
# access record[:year], etc.
end
Your example is, well, clever, but a bit hard to follow.
Gavin
···
On Friday, January 17, 2003, 10:25:38 PM, Tom wrote:
Hi, Gavin,
From: “Gavin Sinclair” gsinclair@soyabean.com.au
Sent: Friday, January 17, 2003 9:03 PM
Isn’t there a CSV processing library available? I imagine an API
would provide hash-like access:
data = CSV.new(filename)
data.each do |record|
# access record[:year], etc.
end
There is a csv processing module here:
http://www.ruby-lang.org/raa/list.rhtml?name=csv
But it doesn’t do anything special treatment about
“the first line as column header.” You must write like below now.
require ‘csv’
year = 5
CSV.open(filename, “r”) do |record|
p record[year]
end
Regards,
// NaHi
Hi,
Isn’t there a CSV processing library available? I imagine an API
would provide hash-like access:
data = CSV.new(filename)
data.each do |record|
# access record[:year], etc.
end
There is a csv processing module here:
http://www.ruby-lang.org/raa/list.rhtml?name=csv
But it doesn’t do anything special treatment about
“the first line as column header.” You must write like below now.
If it is possible to override Row definition in particular
subclass/instance, you could write like this.
require ‘csv’
csv = CSV.open(filename, “r”)
(csv.row_class = Class.new(CSV::Row)).class_eval do
csv.shift.each_with_index do |name, idx|
define_method(name) {at(idx)}
# class_eval(“def #{name} at(#{idx})”)
end
end
csv.each do |record|
p record.year
end
···
At Thu, 23 Jan 2003 19:43:33 +0900, NAKAMURA, Hiroshi nahi@keynauts.com wrote:
–
Nobu Nakada
Kon’nichiwa, nakada-san,
From: “Nobuyoshi Nakada” nobu.nokada@softhome.net
Sent: Thursday, January 23, 2003 9:43 PM
Isn’t there a CSV processing library available? I imagine an API
would provide hash-like access:
data = CSV.new(filename)
data.each do |record|
# access record[:year], etc.
end
There is a csv processing module here:
http://www.ruby-lang.org/raa/list.rhtml?name=csv
But it doesn’t do anything special treatment about
“the first line as column header.” You must write like below now.
If it is possible to override Row definition in particular
subclass/instance, you could write like this.
[snipped cool snippet]
(He admires) You know everything about Ruby and do anything
you want.
For a person who is not as great as you, like me, I may
add such a function to csv module.
Regards,
// NaHi