Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #2121 > unrolled thread
| Started by | Simon Harrison <simon@simonharrison.net> |
|---|---|
| First post | 2011-04-01 13:38 -0500 |
| Last post | 2011-04-03 04:49 -0500 |
| Articles | 8 — 3 participants |
Back to article view | Back to comp.lang.ruby
Searching a CSV file - beginner seeking help Simon Harrison <simon@simonharrison.net> - 2011-04-01 13:38 -0500
Re: Searching a CSV file - beginner seeking help Simon Harrison <simon@simonharrison.net> - 2011-04-01 16:05 -0500
Re: Searching a CSV file - beginner seeking help Simon Harrison <simon@simonharrison.net> - 2011-04-01 16:26 -0500
Re: Searching a CSV file - beginner seeking help Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-01 16:45 -0500
Re: Searching a CSV file - beginner seeking help Simon Harrison <simon@simonharrison.net> - 2011-04-01 15:55 -0500
Re: Searching a CSV file - beginner seeking help Simon Harrison <simon@simonharrison.net> - 2011-04-02 02:39 -0500
Re: Searching a CSV file - beginner seeking help marco <marcofognog@gmail.com> - 2011-04-02 05:44 -0700
Re: Searching a CSV file - beginner seeking help Simon Harrison <simon@simonharrison.net> - 2011-04-03 04:49 -0500
| From | Simon Harrison <simon@simonharrison.net> |
|---|---|
| Date | 2011-04-01 13:38 -0500 |
| Subject | Searching a CSV file - beginner seeking help |
| Message-ID | <9ac7bd74c5f7cbe9b248a3506089d995@ruby-forum.com> |
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
--
Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Simon Harrison <simon@simonharrison.net> |
|---|---|
| Date | 2011-04-01 16:05 -0500 |
| Message-ID | <12e41e65b4cc88aab701ce69352b4e22@ruby-forum.com> |
| In reply to | #2121 |
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
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Simon Harrison <simon@simonharrison.net> |
|---|---|
| Date | 2011-04-01 16:26 -0500 |
| Message-ID | <da66134a88336cf68c2723e114bf6329@ruby-forum.com> |
| In reply to | #2121 |
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
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Jesús Gabriel y Galán <jgabrielygalan@gmail.com> |
|---|---|
| Date | 2011-04-01 16:45 -0500 |
| Message-ID | <AANLkTinm0kTd-ugLkL8xGirGoW2PsCvHyCgvmf8ATCot@mail.gmail.com> |
| In reply to | #2132 |
On Fri, Apr 1, 2011 at 11:26 PM, Simon Harrison <simon@simonharrison.net> wrote:
> 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
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
> 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
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.
[toc] | [prev] | [next] | [standalone]
| From | Simon Harrison <simon@simonharrison.net> |
|---|---|
| Date | 2011-04-01 15:55 -0500 |
| Message-ID | <62cd61aa183266afd5e4fc29165df7fc@ruby-forum.com> |
| In reply to | #2121 |
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>
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Simon Harrison <simon@simonharrison.net> |
|---|---|
| Date | 2011-04-02 02:39 -0500 |
| Message-ID | <5564df27738b659b2ec397a843d4878a@ruby-forum.com> |
| In reply to | #2121 |
Thanks Jesus. I plan to improve script over coming days. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | marco <marcofognog@gmail.com> |
|---|---|
| Date | 2011-04-02 05:44 -0700 |
| Message-ID | <7bc53521-8138-4636-b150-e84ac7b351df@i14g2000yqe.googlegroups.com> |
| In reply to | #2149 |
On Apr 2, 4:39 am, Simon Harrison <si...@simonharrison.net> 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
[toc] | [prev] | [next] | [standalone]
| From | Simon Harrison <simon@simonharrison.net> |
|---|---|
| Date | 2011-04-03 04:49 -0500 |
| Message-ID | <3c138352e4e396bd9f86d83b46a1b1e6@ruby-forum.com> |
| In reply to | #2121 |
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. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web