[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to search words starting with a particular char?

Toki Toki

5/2/2008 5:31:00 PM

Hi to all!

I need to search how many words are starting with a specific character
in a text file, no problem to read the file, but after that I split it
in words, how can
I search all occurencies that's matching my criteria in the resulting
array? Maybe split isn't the best solution?

I would like an output similar to this:

Number of words starting with ' ' = 12345

Here's the code:

txt = File.read("C:\\text.txt")

list = txt.split

Thanks.

Best regards.
--
Posted via http://www.ruby-....

11 Answers

ThoML

5/2/2008 6:13:00 PM

0

> I need to search how many words are starting with a specific character

A word border can be matched with \b.

Using split + grep:
irb(main)> a = 'foo bar foo bar foo bar'
irb(main)> a.split(/\b/).grep(/^f\w*/)
=> ["foo", "foo", "foo"]
irb(main)> a.split(/\b/).grep(/^f\w*/).size
=> 3

Another solution would be to use scan:
irb(main)> a.scan(/\bf\w*/)
=> ["foo", "foo", "foo"]
irb(main)> a.scan(/\bf\w*/).size
=> 3

Toki Toki

5/2/2008 9:54:00 PM

0

ThoML wrote:
>> I need to search how many words are starting with a specific character
>
> A word border can be matched with \b.
>
> Using split + grep:
> irb(main)> a = 'foo bar foo bar foo bar'
> irb(main)> a.split(/\b/).grep(/^f\w*/)
> => ["foo", "foo", "foo"]
> irb(main)> a.split(/\b/).grep(/^f\w*/).size
> => 3
>
> Another solution would be to use scan:
> irb(main)> a.scan(/\bf\w*/)
> => ["foo", "foo", "foo"]
> irb(main)> a.scan(/\bf\w*/).size
> => 3

Thanks a lot for the helpful and fast answer.

Best regards.

--
Posted via http://www.ruby-....

smorgas

10/28/2009 6:44:00 PM

0

On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
wrote:

>
>
>
>
>
>>=============================================================================================
>
>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>
>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>
>>Its still a free country...and if someone doesnt want to hire a nigger
>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>human working for them....more power to em.
>>
>>Now..if you have a federal contract...then you MUST have 10 percent
>>minorites working for you...
>
>>THAT is why there are so many niggas employed in Washington DC in the
>>Goverment.
>
>>Otherwise...we could use trained monkeys to do the same and save some
>>bucks.
>>
>>love ya
>>hank

smorgas

10/28/2009 6:44:00 PM

0

On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
wrote:

>
>
>
>
>
>>=============================================================================================
>
>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>
>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>
>>Its still a free country...and if someone doesnt want to hire a nigger
>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>human working for them....more power to em.
>>
>>Now..if you have a federal contract...then you MUST have 10 percent
>>minorites working for you...
>
>>THAT is why there are so many niggas employed in Washington DC in the
>>Goverment.
>
>>Otherwise...we could use trained monkeys to do the same and save some
>>bucks.
>>
>>love ya
>>hank

smorgas

10/28/2009 6:45:00 PM

0

On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
wrote:

>
>
>
>
>
>>=============================================================================================
>
>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>
>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>
>>Its still a free country...and if someone doesnt want to hire a nigger
>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>human working for them....more power to em.
>>
>>Now..if you have a federal contract...then you MUST have 10 percent
>>minorites working for you...
>
>>THAT is why there are so many niggas employed in Washington DC in the
>>Goverment.
>
>>Otherwise...we could use trained monkeys to do the same and save some
>>bucks.
>>
>>love ya
>>hank

smorgas

10/28/2009 6:45:00 PM

0

On Wed, 28 Oct 2009 12:44:32 -0600, smorgas@board.com
wrote:

>On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
>wrote:
>
>>
>>
>>
>>
>>
>>>=============================================================================================
>>
>>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>>
>>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>>
>>>Its still a free country...and if someone doesnt want to hire a nigger
>>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>>human working for them....more power to em.
>>>
>>>Now..if you have a federal contract...then you MUST have 10 percent
>>>minorites working for you...
>>
>>>THAT is why there are so many niggas employed in Washington DC in the
>>>Goverment.
>>
>>>Otherwise...we could use trained monkeys to do the same and save some
>>>bucks.
>>>
>>>love ya
>>>hank

smorgas

10/28/2009 6:45:00 PM

0

On Wed, 28 Oct 2009 12:44:32 -0600, smorgas@board.com
wrote:

>On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
>wrote:
>
>>
>>
>>
>>
>>
>>>=============================================================================================
>>
>>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>>
>>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>>
>>>Its still a free country...and if someone doesnt want to hire a nigger
>>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>>human working for them....more power to em.
>>>
>>>Now..if you have a federal contract...then you MUST have 10 percent
>>>minorites working for you...
>>
>>>THAT is why there are so many niggas employed in Washington DC in the
>>>Goverment.
>>
>>>Otherwise...we could use trained monkeys to do the same and save some
>>>bucks.
>>>
>>>love ya
>>>hank

smorgas

10/28/2009 6:45:00 PM

0

On Wed, 28 Oct 2009 12:44:32 -0600, smorgas@board.com
wrote:

>On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
>wrote:
>
>>
>>
>>
>>
>>
>>>=============================================================================================
>>
>>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>>
>>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>>
>>>Its still a free country...and if someone doesnt want to hire a nigger
>>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>>human working for them....more power to em.
>>>
>>>Now..if you have a federal contract...then you MUST have 10 percent
>>>minorites working for you...
>>
>>>THAT is why there are so many niggas employed in Washington DC in the
>>>Goverment.
>>
>>>Otherwise...we could use trained monkeys to do the same and save some
>>>bucks.
>>>
>>>love ya
>>>hank

smorgas

10/28/2009 6:45:00 PM

0

On Wed, 28 Oct 2009 12:44:32 -0600, smorgas@board.com
wrote:

>On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
>wrote:
>
>>
>>
>>
>>
>>
>>>=============================================================================================
>>
>>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>>
>>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>>
>>>Its still a free country...and if someone doesnt want to hire a nigger
>>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>>human working for them....more power to em.
>>>
>>>Now..if you have a federal contract...then you MUST have 10 percent
>>>minorites working for you...
>>
>>>THAT is why there are so many niggas employed in Washington DC in the
>>>Goverment.
>>
>>>Otherwise...we could use trained monkeys to do the same and save some
>>>bucks.
>>>
>>>love ya
>>>hank

smorgas

10/28/2009 6:46:00 PM

0

On Wed, 28 Oct 2009 12:45:28 -0600, smorgas@board.com
wrote:

>On Wed, 28 Oct 2009 12:44:32 -0600, smorgas@board.com
>wrote:
>
>>On Wed, 28 Oct 2009 12:43:17 -0600, smorgas@board.com
>>wrote:
>>
>>>
>>>
>>>
>>>
>>>
>>>>=============================================================================================
>>>
>>>On Fri, 11 Apr 2008 08:49:05 -0700 (PDT), Rightwinghank <rightwinghank@hotmail.com> wrote:
>>>
>>>>> On Thu, 10 Apr 2008 09:08:15 -0700 (PDT), pyjamarama
>>>
>>>>Its still a free country...and if someone doesnt want to hire a nigger
>>>>like yourself forinstance...because they dont want a smelly lazy stealing stupid semi
>>>>human working for them....more power to em.
>>>>
>>>>Now..if you have a federal contract...then you MUST have 10 percent
>>>>minorites working for you...
>>>
>>>>THAT is why there are so many niggas employed in Washington DC in the
>>>>Goverment.
>>>
>>>>Otherwise...we could use trained monkeys to do the same and save some
>>>>bucks.
>>>>
>>>>love ya
>>>>hank