string contains one of these???

M

mikkel

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
 
J

James Edward Gray II

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
 
R

Robert Klemme

2006/2/27 said:
Imagine,

I have

leagues=3D%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 ~=3D on it...but is there a simpler way ???
leagues=3D%w{ 1D 2D U16 U19 LR RR JNL NL} =3D> ["1D", "2D", "U16", "U19", "LR", "RR", "JNL", "NL"]
"some stuff NL is chunky".scan( Regexp.new( leagues.join('|') ) )
=3D> ["NL"]

Kind regards

robert
 
D

Daniel Harple

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
 
R

Robert Klemme

Christian said:
Robert Klemme said:
2006/2/27 said:
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
 
M

Mikkel Bruun

amazing...

thanks a bunch everybody...


Christian said:
Robert Klemme said:
2006/2/27, mikkel <[email protected]>:
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)
 
H

hitesh.jasani

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.jasani.org/
 
J

Johan Veenstra

------=_Part_11648_25989153.1141119573141
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

And the winner is ...

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

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

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


- Hitesh
http://www.jasani.org/

------=_Part_11648_25989153.1141119573141--
 
H

hitesh.jasani

Jeffrey, the link should be working for you now. My hosting provider
had a number of servers go belly up earlier in the day.
 
H

hitesh.jasani

Good comments Christian. I thought I'd just hack a set of tests and
post some quick results, but all I think I did was prove that I'm not
supposed to be coding first thing in the morning.

Yes, once you fix that bug in the code by caching the Regexes, they
perform very impressively. In fact, for the modified tests I just ran,
they beat out every other solution in every case except for extremely
large league sizes (> 18,000 elements) where they wouldn't run at all.
But I'm loathe to draw any conclusions from the data just yet. (Burned
once, twice shy?)

There appears to be at least one other bug in the code. One astute,
anonymous person pointed out that the six solutions will not return the
same results for the generated datasets and that preprocessing of input
data could help improve performance even more. I think it all depends
on how one defines the problem as to whether the generated data is
valid input or undefined requirements now being levied on the code.

Mmmm.... I hope JEG II is watching this thread as I'm wondering if
there isn't a rubyquiz in here somewhere.

- Hitesh
http://www.jasani.org/
 
J

James Edward Gray II

Mmmm.... I hope JEG II is watching this thread as I'm wondering if
there isn't a rubyquiz in here somewhere.

If you can think of a good way to spin it:

(e-mail address removed)

James Edward Gray II
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top