csv to hash

O

ou_ryperd

Hi
I have a csv file with two fields per line. What would the most
effective way be to read it into a hash variable ? e.g.:
10000,A
10001,B
10002,C

I have searched ruby-talk and comp.lang.ruby and did not find a
suitable example.
thanks in advance
 
M

Martin DeMello

ou_ryperd said:
Hi
I have a csv file with two fields per line. What would the most
effective way be to read it into a hash variable ? e.g.:
10000,A
10001,B
10002,C

hash = []
IO.foreach('file') {|line|
k,v = line.split(/,/)
hash[k] = v
}

martin
 
S

Simon Strandgaard

I have a csv file with two fields per line. What would the most
effective way be to read it into a hash variable ? e.g.:
10000,A
10001,B
10002,C
=20
I have searched ruby-talk and comp.lang.ruby and did not find a
suitable example.

irb(main):008:0> txt =3D "10000,A\n10001,B\n10002,C"
=3D> "10000,A\n10001,B\n10002,C"
irb(main):009:0> ary =3D txt.scan(/^([^,\n]+),([^,\n]+)/)
=3D> [["10000", "A"], ["10001", "B"], ["10002", "C"]]
irb(main):010:0> h =3D {}
=3D> {}
irb(main):011:0> ary.each {|k,v| h[k] =3D v }
=3D> [["10000", "A"], ["10001", "B"], ["10002", "C"]]
irb(main):012:0> h
=3D> {"10001"=3D>"B", "10002"=3D>"C", "10000"=3D>"A"}
irb(main):013:0>
 
K

kingsley

maybe this will help - it uses the first line in the csv as the handle =20
to access the data e.g. -

myname,myage
kingsley,29
dave,28

d =3D Datahandler.new("data.csv")
d.data[0].myname # returns kingsley
d.data[1].myage # returns 28 (daves age)

class DataHandler

# This allows you to put data in a csv file and access it in =20
your scripts for
# a data driven approach.
#
# e.g.
# require 'datahandler'
# d =3D DataHandler.new("data.csv")
# loginId =3D d.data[0].LoginId --> line 1 of the csv =20
file and column LoginId
# pin =3D d.data[0].Pin --> line 1 of =20
the cvs file and column Pin
#
def initialize(datafile)
@datafile =3D datafile
end
=20
def data
csv_data =3D File.readlines(@datafile)
header =3D csv_data[0].split(",")
header.each{|z| z.chomp!}
data =3D Struct.new("Data", *header)
csv_data.shift
data_array =3D []
csv_data.each do |line|
line.each{|z| line.chomp!}
data_array << data.new(*line.split(","))
end
return data_array
end
=20
end
 
O

ou_ryperd

I must be stoopid:
:6:in `[]=': cannot convert String into Integer (TypeError)
 
O

ou_ryperd

Thanks Simon. No matter what I do I get nil.

txt = File::read("batches.conf")
ary = txt.scan(/^([^,\n]+),([^,\n]+)­/)
h = {}
ary.each {|k,v| h[k] = v }
puts h[10001]

=> nil
 
M

Martin DeMello

ou_ryperd said:
I must be stoopid:
:6:in `[]=': cannot convert String into Integer (TypeError)

Oops, no, my mistake. I meant hash = {} rather than hash = []. {} is an
empty hash; [] is an empty array.

Also, note that your hash keys will be for the form '10000', not 10000 -
if you want the latter, you need to say hash[k.to_i] = v.

martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top