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?
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
I don’t have a Ruby CSV library to play with at the moment, but this
should work as far as I know:
csv.find {|row| row[‘Part No’] == my_part_number }[‘Price’]
thank you Marcus for the reply. where did you save ‘csvtest.csv’ file or
how do you create csvtest.csv file.
It depends what version of Ruby sketchup is using. Have you tried
FasterCSV?
Am 09.07.2013 21:55, schrieb Joel P.:
It depends what version of Ruby sketchup is using. Have you tried
FasterCSV?
Not sure how that should help,
CSV is part of the Ruby standard library since at least 1.8.7,
and not a gem, so it should be included in any decent Ruby installation.
FasterCSV is a gem that at some point replaced and became the stdlib CSV
(I think with 1.9.3), so you do not need it anymore as a gem.
@ Nithi anand:
I do not know which parts of Ruby are available from sketchup,
maybe you should also try on a sketchup mailing list.
Joel P. wrote in post #1114740:
I don’t have a Ruby CSV library to play with at the moment, but this
should work as far as I know:csv.find {|row| row[‘Part No’] == my_part_number }[‘Price’]
Thanks for the reply its works on ruby 2.0 command prompt.but I need use
this in sketchup when I require ‘csv’ in sketchup it’s gives error
message it says no such a file to load. how do I can use it in sketchup.