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:
def load_xvid_file(“person.csv”, “r”) do |csv|) @firstnames = [] @lastnames = []
csv_file = CSV.read(person.csv)
csv_contents.shift
csv_contents.each do |row| @firstnames << row[0]
end
csv_contents.each do |row| @lastnames << row[1]
end @firstnames.each { |f| f.downcase! } @lastnames.each { |l| l.downcase! }
end
csv_contents.shift
csv_contents.each do |row| @firstnames << row[0]
end
csv_contents.each do |row| @lastnames << row[1]
end
Where have you assigned csv_contents? I’m guessing that you want to
pass the name of the file to open with def load_xvid_file. If that is
the case you should be doing
def load_xvid_file(file_to_open) #stuff
end
def search_for_person(fname,lname)
fname = fname.downcase
lname = lname.downcase
puts
What is the puts for?
if @firstnames.include?(fname)
if @lastnames.include?(lname)
You should take a look at Enumerable#select or Array#select if you use
1.9’s default CSV class you will get back an array of arrays or you
can just use an each block. Although I am sure there is a more
compact way of searching a nested array.
puts “#{fname} #{lname} found!”
else
puts “#{fname} #{lname} not found!”
end
prompt
end
Unless I am missing something you need one more end in search_for
What is the error message that you get? In the future always include
a bit about the error you get when you run into problems. Hope this
helps.
On Tue, Nov 15, 2011 at 05:09:46PM +0900, Aksh A. 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
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
-----Messaggio originale-----
Da: Jeremy H. [mailto:[email protected]]
Inviato: mercoled 16 novembre 2011 07:02
A: ruby-talk ML
Oggetto: Re: search through a csv file
On Wed, Nov 16, 2011 at 06:15:33AM +0900, Yossef M. wrote:
thought. I’ve never had a reason to use it before.
Ah, learning something new, loving it :-).
-----Messaggio originale-----
Da: Aksh A. [mailto:[email protected]]
Inviato: marted 15 novembre 2011 09:10
A: ruby-talk ML
Oggetto: search through a csv file
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:
def load_xvid_file(“person.csv”, “r”) do |csv|) @firstnames = [] @lastnames = []
csv_file = CSV.read(person.csv)
csv_contents.shift
csv_contents.each do |row| @firstnames << row[0]
end
csv_contents.each do |row| @lastnames << row[1]
end @firstnames.each { |f| f.downcase! } @lastnames.each { |l| l.downcase! }
end