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
def search_for_person(fname,lname)
fname = fname.downcase
lname = lname.downcase
puts
if @firstnames.include?(fname)
if @lastnames.include?(lname)
puts “#{fname} #{lname} found!”
else
puts “#{fname} #{lname} not found!”
end
prompt
end
this code doesn’t work. can you help me debug this.
Thank you loads in advance.
Regards,
Akshath
On Tue, Nov 15, 2011 at 3:09 AM, Aksh A. [email protected]
wrote:
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
@firstnames.each { |f| f.downcase! }
@lastnames.each { |l| l.downcase! }
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
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
On Wed, Nov 16, 2011 at 06:15:33AM +0900, Yossef M. wrote:
search_criteria.all? { |k, v| row[k] == v }
I actually had to check out Hash#all? to see if it worked the way I
thought. I’ve never had a reason to use it before.
Ah, learning something new, loving it :-).
On Nov 15, 2:14pm, Jeremy H. [email protected] wrote:
matches = csv.find_all do |row|
match = true
search_criteria.keys.each do |key|
match = match && ( row[key] == search_criteria[key] )
end
match
end
search_criteria.all? { |k, v| row[k] == v }
I actually had to check out Hash#all? to see if it worked the way I
thought. I’ve never had a reason to use it before.
-----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 :-).
–
Jeremy H. [email protected]
–
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f
Sponsor:
Conto Arancio al 4,20%. Soldi sempre disponibili, zero spese, aprilo in
due minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid923&d)-12
-----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
def search_for_person(fname,lname)
fname = fname.downcase
lname = lname.downcase
puts
if @firstnames.include?(fname)
if @lastnames.include?(lname)
puts “#{fname} #{lname} found!”
else
puts “#{fname} #{lname} not found!”
end
prompt
end
this code doesn’t work. can you help me debug this.
Thank you loads in advance.
Regards,
Akshath
–
Posted via http://www.ruby-forum.com/.
–
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f
Sponsor:
Conto Arancio al 4,20%. Zero spese e massima liberta’, aprilo in due
minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid922&d)-12