On Tue, Nov 15, 2011 at 05:09:46PM +0900, Aksh Akku wrote:
> i have a person details csv file. I am trying to write a code to search
> for a particular person based on his first and last name. I wrote the
> code to search for single value but i am not sure how to search through
> a table with multiple keys. My code is:

The CSV library that ships with Ruby can help quite a bit with this.
I assume that you are on 1.9, or using FasterCSV with 1.8

https://gist.github.com/1367985

The meat of which is:

    search_criteria =  { 'name' => 'Ruby', 'sex' => 'girl' }
    options = { :headers => :first_row, :converters => [ :numeric ] }
   
    CSV.open( csv_fname, "r", options ) do |csv|

      # Since CSV includes Enumerable we can use 'find_all'
      # which will return all the elements of the Enumerble for 
      # which the block returns true

      matches = csv.find_all do |row|
        match = true
        search_criteria.keys.each do |key|
          match = match && ( row[key] == search_criteria[key] )
        end
        match
      end
    end

enjoy,

-jeremy

-- 
========================================================================
 Jeremy Hinegardner                              jeremy / hinegardner.org