On Feb 11, 2008, at 12:14 PM, Adam S. wrote:
Thank you for all the work that went into producing these quizzes,
James. I’ve really enjoyed them.
Thank you Adam. It’s great to hear from another regular.
James Edward G. II
On Feb 11, 2008, at 12:14 PM, Adam S. wrote:
Thank you for all the work that went into producing these quizzes,
James. I’ve really enjoyed them.
Thank you Adam. It’s great to hear from another regular.
James Edward G. II
What’s the best way to find all occurances of a string in a hash’s
index?
That is,
x = { ‘Hello’ => 1, ‘Goodbye’ => 2, ‘Hello there’ => 3}
How do I most efficiently get 1 and 3 to return on a search for
‘Hello’ in the index of the hash? I know I can iterate over the
index, but is there a better way?
Thanks.
On Feb 11, 3:47 pm, Serg K. [email protected] wrote:
Thanks.
Hi Serg,
Here’s one way to do it:
x = { ‘Hello’ => 1, ‘Goodbye’ => 2, ‘Hello there’ => 3}
result = x.keys.select { |k| k.include? “Hello” }.map { |k| x[k] }
p result
There’s really no way to avoid iterating over the keys in the hash.
In this case, Enumerable#select is doing that when it produces an
array of the keys that contain “Hello”. That array of keys is
converted into an array of values by using Enumerable#map.
Eric
P.S. It looks like you replied to a posting for Ruby Q. #156. Since
this is unrelated to the Ruby Q., it would have been preferable to
make a new post.
====
Are you interested in on-site Ruby training that uses well-designed,
real-world, hands-on exercises? http://LearnRuby.com
oops i took the easy way and replied. sorry.
Thanks for the help/explanation. I was hoping there was a shorter/
more efficient way than iterating maybe a nested hash of some sort.
Guess I’ll settle for what we have.
Thanks much.
On Feb 11, 2008 4:24 PM, Eric I. [email protected] wrote:
‘Hello’ in the index of the hash? I know I can iterate over the
result = x.keys.select { |k| k.include? “Hello” }.map { |k| x[k] }
p result
I could only come up with an iterative solution also…
h = Hash[‘Hello’, 1, ‘Goodbye’, 2, ‘Hello there’, 3]
a = h.select {|k, v| k=~/Hello/}.map {|v| v[1]}
p a
2008/2/12, Serg K. [email protected]:
oops i took the easy way and replied. sorry.
Thanks for the help/explanation. I was hoping there was a shorter/
more efficient way than iterating maybe a nested hash of some sort.
Nested Hashes would only help if you would know beforehand according
to which criteria you want to split up your strings. If you are doing
it on word basis you could do this
require ‘set’
word_index = Hash.new {|h,k| h[k] = Set.new}
x.each_key {|k| k.downcase.scan(/\w+/) {|wd| word_index[wd] << k}}
Note though that you have to update word_index whenever x changes.
Well, there are indexing structures optimized for text retrieval (your
favorite search engine is using those). However, I have no idea
whether there are libs that implement those in Ruby. You could try to
check RAA at http://raa.ruby-lang.org/ for such libs.
Kind regards
robert
On Feb 12, 2008 5:24 PM, Todd B. [email protected] wrote:
I could only come up with an iterative solution also…
h = Hash[‘Hello’, 1, ‘Goodbye’, 2, ‘Hello there’, 3]
a = h.select {|k, v| k=~/Hello/}.map {|v| v[1]}
p a
Another one just for fun…
h.map {|k,v| v if k=~/Hello/}.compact
Todd
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs