Counting Occurrences of a String in an Array

S

shrouded.shoyu

Hi,

I'm pretty new to Ruby, and I've been trying to figure out the best
way to do this with little luck.

I have an array that is all strings, and I need to count, for each
string in the array, how many times some substring appears in it.

So, for example, my array is ["aaa", "aab", "abb", "bbb"] and I want
to return a new array that tells me how many times "a" appears: [3, 2,
1, 0]

Can anyone point me in the right direction with this?

Thanks!
 
R

Rodrigo Bermejo

unknown said:
Hi,

I'm pretty new to Ruby, and I've been trying to figure out the best
way to do this with little luck.

I have an array that is all strings, and I need to count, for each
string in the array, how many times some substring appears in it.

So, for example, my array is ["aaa", "aab", "abb", "bbb"] and I want
to return a new array that tells me how many times "a" appears: [3, 2,
1, 0]

Can anyone point me in the right direction with this?

Thanks!

Sounds like greping !

["aaa", "aab", "abb", "bbb"].grep ....half of the answer.
 
D

David A. Black

Hi --

Hi,

I'm pretty new to Ruby, and I've been trying to figure out the best
way to do this with little luck.

I have an array that is all strings, and I need to count, for each
string in the array, how many times some substring appears in it.

So, for example, my array is ["aaa", "aab", "abb", "bbb"] and I want
to return a new array that tells me how many times "a" appears: [3, 2,
1, 0]

Can anyone point me in the right direction with this?

array.map {|str| str.count('a') }


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!
 
F

F. Senault

Le 10 février 2009 à 22:51, (e-mail address removed) a écrit :
So, for example, my array is ["aaa", "aab", "abb", "bbb"] and I want
to return a new array that tells me how many times "a" appears: [3, 2,
1, 0]

When you need to map elements of an array to another array, the method
to use is, no surprise here, map (a/k/a collect).

Now, finding the number occurences of a string inside another one can be
done in a few ways, depending on what you mean exactly. Scan is one :
[ "aaa", "aab", "abb", "bbb" ].map { |x| x.scan(/a/).length }
=> [3, 2, 1, 0]

Fred
 
T

Tim Hunter

Hi,

I'm pretty new to Ruby, and I've been trying to figure out the best
way to do this with little luck.

I have an array that is all strings, and I need to count, for each
string in the array, how many times some substring appears in it.

So, for example, my array is ["aaa", "aab", "abb", "bbb"] and I want
to return a new array that tells me how many times "a" appears: [3, 2,
1, 0]

Can anyone point me in the right direction with this?

Thanks!

Looks like a job for collect:

irb(main):008:0> a = ["aaa", "aab", "abb", "bbb"]
=> ["aaa", "aab", "abb", "bbb"]
irb(main):009:0> a.collect {|v| v.scan(/a/).size}
=> [3, 2, 1, 0]
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top