From: Robert Klemme Date: 2006-02-03T19:18:29+09:00 Subject: Re: Seeking the Ruby way William James wrote: > Todd Breiholz wrote: >> I'm just getting my feet wet with Ruby and would like some advice on >> how you "old-timers" would write the following script using Ruby >> idioms. >> >> The intent of the script is to parse a CSV file that contains 2 >> fields per row, sorted on the second field. There may be multiple >> rows for field 2. I want to get a list of all of the unique values >> of field2 that has more than 1 value for the 1st 6 characters of >> field 1. > > --- input data ----- > 123456ab,900 > 123456cd,900 > 123456ef,909 > 012345gh,909 > --- end of input ----- > > --- Using a hash of arrays: > > require 'csv' > > h = Hash.new{ [] } I wonder how this works since the Hash never stores these arrays. > CSV::Reader.parse(File.open( ARGV.first )) { |row| > h[row.last] |= [ row.first[0,6] ] } > p h.delete_if{|k,v| v.size == 1 } > > --- output ----- > {"909"=>["123456", "012345"]} > --- end of output ----- Is this really the output of the script above? robert