From: Simon Strandgaard Date: 2005-08-02T18:08:24+09:00 Subject: Re: csv to hash On 8/2/05, ou_ryperd wrote: > 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. irb(main):008:0> txt = "10000,A\n10001,B\n10002,C" => "10000,A\n10001,B\n10002,C" irb(main):009:0> ary = txt.scan(/^([^,\n]+),([^,\n]+)/) => [["10000", "A"], ["10001", "B"], ["10002", "C"]] irb(main):010:0> h = {} => {} irb(main):011:0> ary.each {|k,v| h[k] = v } => [["10000", "A"], ["10001", "B"], ["10002", "C"]] irb(main):012:0> h => {"10001"=>"B", "10002"=>"C", "10000"=>"A"} irb(main):013:0> -- Simon Strandgaard