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{ [] }
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  -----


---  Using a hash of hashes:

require 'csv'

h = Hash.new{|h,k| h[k] = {} }
CSV::Reader.parse(File.open( ARGV.first )) { |row|
  h[row.last][ row.first[0,6] ] = 8 }
p h.delete_if{|k,v| v.size == 1 }

---  output  -----
{"909"=>{"012345"=>8, "123456"=>8}}
---  end of output  -----