Am 08.07.2013 15:13, schrieb Nithi anand:
> I need to perform a lookup on a CSV file.
> The CSV file has 2 columns: Part No, Price
> Given a specific Part No, I need to find the corresponding Price.
> What would be the appropriate CSV method?

$ cat csvtest.csv
Part No,Price
11,1
13,2
17,3
19,4

$ irb
2.0.0p247 :001 > require 'csv'
  => true
2.0.0p247 :002 > csv = CSV.read('csvtest.csv', :headers => true)
  => #<CSV::Table mode:col_or_row row_count:5>
2.0.0p247 :003 > part = csv.find {|row| row['Part No'] == '11' }
  => #<CSV::Row "Part No":"11" "Price":"1">
2.0.0p247 :004 > part['Price']
  => "1"


Regards,
Marcus


-- 
<https://github.com/stomar/>