[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

string contains one of these???

Mikkel Bruun

2/27/2006 1:36:00 PM

Imagine,

I have

leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}

for a given string, say "some stuff NL is chunky" i want determine which
of the matches it contains...

now, the hard way (more code, less thought) would be to iterate the
array and do a ~= on it...but is there a simpler way ???

thanks in advance

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


16 Answers

James Gray

2/27/2006 1:43:00 PM

0

On Feb 27, 2006, at 7:35 AM, mikkel wrote:

> Imagine,
>
> I have
>
> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>
> for a given string, say "some stuff NL is chunky" i want determine
> which
> of the matches it contains...
>
> now, the hard way (more code, less thought) would be to iterate the
> array and do a ~= on it...but is there a simpler way ???

The hard way isn't too hard and doesn't require but a line of code:

>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
>> str="some stuff NL is chunky"
=> "some stuff NL is chunky"
>> leagues.find_all { |league| str.include? league }
=> ["NL"]

Hope that helps.

James Edward Gray II



Robert Klemme

2/27/2006 1:43:00 PM

0

2006/2/27, mikkel <mikkel@helenius.dk>:
> Imagine,
>
> I have
>
> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>
> for a given string, say "some stuff NL is chunky" i want determine which
> of the matches it contains...
>
> now, the hard way (more code, less thought) would be to iterate the
> array and do a ~= on it...but is there a simpler way ???

>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
=> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
>> "some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
=> ["NL"]

Kind regards

robert

--
Have a look: http://www.flickr.com/photos/fu...


Daniel Harple

2/27/2006 1:44:00 PM

0

On Feb 27, 2006, at 2:35 PM, mikkel wrote:

> now, the hard way (more code, less thought) would be to iterate the
> array and do a ~= on it...but is there a simpler way ???

How about:

leagues = %w{1D 2D U16 U19 LR RR JNL NL}
words = "some stuff NL is chunky".split
leagues.select { |m| words.include?(m) } # => ["NL"]

-- Daniel


Christian Neukirchen

2/27/2006 4:04:00 PM

0

"Robert Klemme" <shortcutter@googlemail.com> writes:

> 2006/2/27, mikkel <mikkel@helenius.dk>:
>> Imagine,
>>
>> I have
>>
>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>>
>> for a given string, say "some stuff NL is chunky" i want determine which
>> of the matches it contains...
>>
>> now, the hard way (more code, less thought) would be to iterate the
>> array and do a ~= on it...but is there a simpler way ???
>
>>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
> => ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
>>> "some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
> => ["NL"]

irb(main):002:0> "some stuff NL is chunky".scan Regexp.union(*leagues)
=> ["NL"]

> robert
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Robert Klemme

2/27/2006 5:49:00 PM

0

Christian Neukirchen wrote:
> "Robert Klemme" <shortcutter@googlemail.com> writes:
>
>> 2006/2/27, mikkel <mikkel@helenius.dk>:
>>> Imagine,
>>>
>>> I have
>>>
>>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>>>
>>> for a given string, say "some stuff NL is chunky" i want determine
>>> which of the matches it contains...
>>>
>>> now, the hard way (more code, less thought) would be to iterate the
>>> array and do a ~= on it...but is there a simpler way ???
>>
>>>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>> => ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
>>>> "some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
>> => ["NL"]
>
> irb(main):002:0> "some stuff NL is chunky".scan
> Regexp.union(*leagues) => ["NL"]

Even better! Didn't know about that method. Learn something new every
day. Thanks!

robert

Stefan Lang

2/27/2006 9:01:00 PM

0

amazing...

thanks a bunch everybody...


On Tuesday, February 28, 2006, at 2:53 AM, Robert Klemme wrote:
>Christian Neukirchen wrote:
>> "Robert Klemme" <shortcutter@googlemail.com> writes:
>>
>>> 2006/2/27, mikkel <mikkel@helenius.dk>:
>>>> Imagine,
>>>>
>>>> I have
>>>>
>>>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>>>>
>>>> for a given string, say "some stuff NL is chunky" i want determine
>>>> which of the matches it contains...
>>>>
>>>> now, the hard way (more code, less thought) would be to iterate the
>>>> array and do a ~= on it...but is there a simpler way ???
>>>
>>>>> leagues=%w{ 1D 2D U16 U19 LR RR JNL NL}
>>> => ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
>>>>> "some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
>>> => ["NL"]
>>
>> irb(main):002:0> "some stuff NL is chunky".scan
>> Regexp.union(*leagues) => ["NL"]
>
>Even better! Didn't know about that method. Learn something new every
>day. Thanks!
>
> robert
>
>


Mikkel Bruun

www.strongside.dk - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk - Buy Old Stuff!(DK)




--
Posted with http://De.... Sign up and save your time!


hitesh.jasani@gmail.com

2/28/2006 5:13:00 AM

0

You've got a bunch of great answers already, but here's another option.

leagues = %w(1D 2D U16 U19 LR RR JNL NL)
words = "some stuff NL is chunky"

irb(main):008:0> words.split & leagues
=> ["NL"]


- Hitesh
http://www.j...

Johan Veenstra

2/28/2006 9:40:00 AM

0

And the winner is ...

On 2/28/06, hitesh.jasani@gmail.com <hitesh.jasani@gmail.com> wrote:
>
> You've got a bunch of great answers already, but here's another option.
>
> leagues = %w(1D 2D U16 U19 LR RR JNL NL)
> words = "some stuff NL is chunky"
>
> irb(main):008:0> words.split & leagues
> => ["NL"]
>
>
> - Hitesh
> http://www.j...
>
>
>

hitesh.jasani@gmail.com

2/28/2006 1:33:00 PM

0

Actually if you flip it around as 'leagues & words.split' it turns out
to have some significant performance advantages in many cases. See
http://www.j...articles/2006/02/28/adding-the-science-back-to-comput...
for more details.

- Hitesh
http://www.j...

Jeff Schwab

2/28/2006 4:24:00 PM

0

hitesh.jasani@gmail.com wrote:
> Actually if you flip it around as 'leagues & words.split' it turns out
> to have some significant performance advantages in many cases. See
> http://www.j...articles/2006/02/28/adding-the-science-back-to-comput...
> for more details.
>
> - Hitesh
> http://www.j...

Is it possible that link is incorrect?

"Firefox can't establish a connection to the server at www.jasani.org."

I'm dying to learn about this now. :)