simple noob hash w/ arrays for values question

P

p-hawk

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?
 
K

katherine

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

p-hawk

katherine said:
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*
 
E

Erik Veenstra

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top