Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > simple noob hash w/ arrays for values question

Reply
Thread Tools

simple noob hash w/ arrays for values question

 
 
p-hawk
Guest
Posts: n/a
 
      07-05-2006
Sorry for the simple question, but I'm new to programming and have been
banging my head over this.

I've got myself a hash with each value an array of strings. I want to
grep the strings and collect only the strings that match the regexp
into an array. How do I go about this?

 
Reply With Quote
 
 
 
 
katherine
Guest
Posts: n/a
 
      07-05-2006
my_result_array = []
my_hash.each_value do |my_array|
my_result_array += my_array.select { |item| item =~ /^[A-Za-z]+$/ }
end

The code above would go through and put all of the strings that only
consisted of letters into one array. Is something like this what you
were looking for?

p-hawk wrote:
> Sorry for the simple question, but I'm new to programming and have been
> banging my head over this.
>
> I've got myself a hash with each value an array of strings. I want to
> grep the strings and collect only the strings that match the regexp
> into an array. How do I go about this?


 
Reply With Quote
 
 
 
 
p-hawk
Guest
Posts: n/a
 
      07-05-2006

katherine wrote:
> my_result_array = []
> my_hash.each_value do |my_array|
> my_result_array += my_array.select { |item| item =~ /^[A-Za-z]+$/ }
> end
>
> The code above would go through and put all of the strings that only
> consisted of letters into one array. Is something like this what you
> were looking for?


Yes, this is exactly what I was looking for. Thank you for your help.

*/me goes off to conquer the world*

 
Reply With Quote
 
Erik Veenstra
Guest
Posts: n/a
 
      07-06-2006
> I've got myself a hash with each value an array of strings. I
> want to grep the strings and collect only the strings that
> match the regexp into an array. How do I go about this?


my_array = my_hash.values.flatten.select{|s| s =~ /.../}

gegroet,
Erik V. - http://www.erikveen.dds.nl/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash of Hash of Arrays Question Älphä Blüë Ruby 5 07-18-2009 07:36 PM
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57