F. Senault
2/10/2009 10:05:00 PM
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
--
Perl already has _bless_, and we know what it does... [it] should also
have _smite_, and we know what it should do, too. If more languages had
_smite_ implemented, the remaining programmers would be better than the
current average. (Mike Andrews in the SDM)