Forum: Ruby Counting Occurrences of a String in an Array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
191b7e32e98a8ae66763bb0574253468?d=identicon&s=25 unknown (Guest)
on 2009-02-10 22:55
(Received via mailing list)
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!
63529e6fbb35dde336d471b569b84d87?d=identicon&s=25 Rodrigo Bermejo (rbermejo)
on 2009-02-10 23:02
unknown wrote:
> 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.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 23:09
(Received via mailing list)
Hi --

On Wed, 11 Feb 2009, shrouded.shoyu@gmail.com wrote:

> 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!
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-02-10 23:13
(Received via mailing list)
shrouded.shoyu@gmail.com wrote:
> 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]
B57c5af36f5c1f33243dd8b2dd9043b1?d=identicon&s=25 F. Senault (Guest)
on 2009-02-10 23:13
(Received via mailing list)
Le 10 février 2009 à 22:51, shrouded.shoyu@gmail.com 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
This topic is locked and can not be replied to.