On Mon, 31 Oct 2005, NAKAMURA, Hiroshi wrote:

> Interesting.  Do you have any idea how API should be?
>
> # rambling
>  reader = CSV::Reader.create(STDIN)
>  reader.set_initial_row_as_a_column_header = true # ???
>  reader.each do |row|
>    p row[:name], row[:age]
>  end
>  reader.close
>
>  CSV.parse_with_initial_row_as_a_column_header(STDIN) do |row|
>    ...
>  end
>
>  CSV.parse_with_specified_colname(STDIN, [:name, :dept, :age]) do |row|
>    p row[:name], row[:age]
>  end

hmmm.  maybe somthing simple like

   #
   # assume first row as fields.  slurp.
   #
     table = CSV::Table::new io

   #
   # specify fields.  slurp.
   #
     table = CSV::Table::new io, 'fields' => %w[ name dept age ]

   #
   # assume first row as fields.  iterator.
   #
     CSV::Table::parse(io) do
       p row
       p row.fields
     end

   #
   # specify fields.  iterator.
   #
     CSV::Table::parse(io, 'fields' => %w[ name dept age ]) do
       p row
       p row.fields
     end


the options hash could by extended to accept something like type info later.


example

   harp:~ > cat b.rb
   require 'csv'
   require 'arrayfields'

   class CSV
     class Table < ::Array
       def initialize arg, opts = {}
         fields = opts.values_at('fields', :fields).first
         CSV::parse(arg) do |row|
           fields = row and next unless fields
           row.fields = fields
           self << row
         end
       end
       def sort_by! *a, &b
         replace(sort_by(*a, &b))
       end
     end
   end

   csv = <<-csv
   name,dept,age
   sara,planning,24
   john,security,55
   csv

   table = CSV::Table::new csv

   p table.first['name']
   p table.last['name']

   table.sort_by!{|row| row['name']}

   p table.first['name']
   p table.last['name']


   harp:~ > ruby b.rb
   "sara"
   "john"
   "john"
   "sara"

regards.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================