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.
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 :
3) Working on something for you.
---- Simon Harrison <simon@simonharrison.net> wrote:
···
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.
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
case answer
when /y/
search_for_film
when /n/
puts "Goodbye."
exit
else
prompt
end
end
load_xvid_file("movie.csv")
search_for_film
---- Simon Harrison <simon@simonharrison.net> wrote:
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.
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>
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.
···
On Fri, Apr 1, 2011 at 11:26 PM, Simon Harrison <simon@simonharrison.net> wrote:
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
···
On Apr 2, 4:39 am, Simon Harrison <si...@simonharrison.net> wrote:
Thanks Jesus. I plan to improve script over coming days.