Searching a CSV file - beginner seeking help

Hi all. I’ve written a little script to search a csv file for films. It
works but has problems. I’d appreciate any pointers with the following:

  1. I’ve used instance variables without a class, to make them visible
    amongst methods. Is this acceptable for a short script like this?

  2. As it is, it will only match exactly. For example, I have The Mummy
    and The Mummy Returns. If I search for ‘mummy’ program reports “film not
    found.” I know I need to use Regexp’s, not sure how to implement it here
    though.

  3. Any other comments gratefully received.

#################

search_films.rb

#################

require ‘csv’

def load_xvid_file(path_to_csv)
@names = []
csv_contents = CSV.read(path_to_csv)
csv_contents.shift
csv_contents.each do |row|
@names << row[0]
end
@names.each { |f| f.downcase! }
end

def search_for_film
print "Enter name of film to search for: "
film = gets.chomp.downcase
puts

if @names.include?(film)
puts “#{film} found!”
else
puts “#{film} not found :(”
end
prompt
end

def prompt
print "Search again? (y or n) "
answer = gets.chomp.downcase

case answer
when /y/
search_for_film
when /n/
puts “Goodbye.”
exit
else
prompt
end
end

load_xvid_file("/home/simon/Documents/CSV/XviD.csv")
search_for_film

OK

  1. I would make this as modular as you can, that way you can expand it
    when needed.

  2. When you do your “if @names.include?(film)” is where you would do
    your regx search.
    Here is a link or so :
    http://www.regextester.com/
    Regular Expressions Reference

  3. Working on something for you.

Here you go. This is VERY rough, but it should get you headed in the
right direction

#################

search_films.rb

#################

require ‘csv’

def load_xvid_file(path_to_csv)
@names = []
csv_contents = CSV.read(path_to_csv)
csv_contents.shift
csv_contents.each do |row|
@names << row[0]
end
@names.each { |f| f.downcase! }
#puts @names
end

def search_for_film
@tick = 0
#puts @names
print "Enter name of film to search for: "
$film = gets.chomp.downcase
puts

@names.each do |$check|
puts $check , @tick

 if (/#{$film}(.*)|(.*)#{$film}/i =~ $check)
   @tick = 1
   #puts "TEST"
 end

end
if(@tick == 1)
puts “#{$film} found!”
else
puts “#{$film} not found :(”
end
prompt
end

def prompt
print "Search again? (y or n) "
answer = gets.chomp.downcase

case answer
when /y/
search_for_film
when /n/
puts “Goodbye.”
exit
else
prompt
end
end

load_xvid_file(“movie.csv”)
search_for_film

Got it!

def search_for_film
print "Enter name of film to search for: "
film = gets.chomp.downcase

results = @films.grep(/#{film}/)
if results
puts results
menu
elsif results.empty?
puts “Nothing found.”
menu
else
menu
end
end

Sorry, don’t know your name. You’re just appearing as ‘guest’. Are you
sure it’s a good idea to have globals in block variables? I’ve been
continuing to play and the following seems to be what I’m after. Sadly
nothing prints out and the program exits.

def search_for_film
print "Enter name of film to search for: "
film = gets.chomp.downcase

results = @films.grep(/film/)
if results
print results
elsif results.empty?
puts “Nothing found.”
else
menu
end
end

Here is irb demonstrating that it should work!

irb(main):009:0> films = [‘the mummy’, ‘the mummy returns’, ‘the mummy
7’, ‘the daddy’]
=> [“the mummy”, “the mummy returns”, “the mummy 7”, “the daddy”]
irb(main):010:0> films.grep(/mummy/)
=> [“the mummy”, “the mummy returns”, “the mummy 7”]
irb(main):011:0>

This works perfectly for me now. In case anyone may benefit from it:

require ‘csv’

def load_xvid_file(path_to_csv)
@films = []
csv_contents = CSV.read(path_to_csv)
csv_contents.shift
csv_contents.each do |row|
@films << row[0]
end
@films.each { |f| f.downcase! }
end

def search_for_film
print "Enter name of film to search for: "
film = gets.chomp.downcase

results = [@films.grep(/#{film}/)]
if results
results.each { |f| puts f }
prompt
else
puts “Nothing found.”
prompt
end
end

def prompt
print "Search again? (y or n) "
answer = gets.chomp.downcase

case answer
when /^y/
search_for_film
when /^n/
puts “Goodbye.”
exit
else
prompt
end
end

load_xvid_file("/home/simon/Documents/CSV/XviD.csv")
search_for_film

Thanks Jesus. I plan to improve script over coming days.

On Fri, Apr 1, 2011 at 11:26 PM, Simon H.
[email protected] wrote:

@films << row[0]
end
@films.each { |f| f.downcase! }
end

def search_for_film
print "Enter name of film to search for: "
film = gets.chomp.downcase

results = [@films.grep(/#{film}/)]
if results

this will always be true, since you are initializing results to an
array. Enumerable#grep already returns an array, so I’d do:

results = @films.grep(/#{film}/)
if results.empty?
puts “nothing found”
else
results.each …

if results

answer = gets.chomp.downcase
end

load_xvid_file(“/home/simon/Documents/CSV/XviD.csv”)
search_for_film

By the way, I’d change a bit around the logic and the user interface,
you seem to have both a little bit mixed up. I would call method
prompt from the main script, in that method I would ask the user for a
word and call search_for_film passing what the user typed. From that
method I would return the results array. Back in the prompt method, I
would print the results and loop for another run. This way,
search_for_film is not tied to the specific user interaction and is
more general and easier to refactor and reuse.

Jesus.

Thanks marco. Looks like it has potential. I can’t think how it would
really help me with this little project but thanks for the link.

On Apr 2, 4:39am, Simon H. [email protected] wrote:

Thanks Jesus. I plan to improve script over coming days.


Posted viahttp://www.ruby-forum.com/.

Hi Simon, just a tip, there is a ruby library to deal with CSV data in
a way very similar to ActiveRecord, but just doesn’t work with ruby
1.9.
https://github.com/marcofognog/active_csv

Regards