From: Drew Olson Date: 2007-02-07T00:36:27+09:00 Subject: Re: Parse csv similar file Gavin Kistner wrote: > On Feb 6, 7:32 am, "Rebhan, Gilbert" > wrote: >> i want to get a collection for every E followed by digits, >> so with the example above, i want to get = > > lines = DATA.readlines.map{ |line| > line.chomp.split( ';' ) > } > lookup = {} > lines.each{ |data| > key = data.find{ |value| /^E/ =~ value } > lookup[ key ] = data > } > p lookup[ "E050441" ] > #=> ["AP850SDS", "INCLIB", "E050441", "AP013", "240107", "0730"] > __END__ > AP850KP;INCLIB;E023889;AP013;240107;0730 > AP850SD$;INCLIB;E052337;AP013;240107;0730 > AP850SDA;INCLIB;E050441;AP013;240107;0730 > AP850SDI;INCLIB;E023889;AP013;240107;0730 > AP850SDO;INCLIB;E052337;AP013;240107;0730 > AP850SDS;INCLIB;E050441;AP013;240107;0730 I think he wants to append this array with information each time he sees the same key, so modify your code like so: lines = DATA.readlines.map{ |line| line.chomp.split( ';' ) } lookup = {} lines.each{ |data| key = data.find{ |value| /^E/ =~ value } lookup[ key ] ||= [] lookup[ key ] << data } -- Posted via http://www.ruby-forum.com/.