Find in Array

B

Benjamin S.

Hey everbody

I searched like for 2 hours now and I didn't find anything. Can anyone
tell me, how i search an array? I got an array of strings and have to
search these. How do I manage to do this? Thx
 
J

Jesús Gabriel y Galán

As I see it, with these methods i can only search for full strings. My
problem is, that i get a string and my search parameter is only a part
of this string (so find, include, find_index etc doesn't work...)

You can use find/select with a regular expression:


irb(main):037:0> a = %w{test test2 nothing something}
=> ["test", "test2", "nothing", "something"]
irb(main):038:0> a.find {|x| /test/ =~ x}
=> "test"
irb(main):039:0> a.select {|x| /test/ =~ x}
=> ["test", "test2"]
irb(main):040:0> a.select {|x| /thing/ =~ x}
=> ["nothing", "something"]

Jesus.
 
R

Robert Klemme

2011/2/7 Jes=FAs Gabriel y Gal=E1n said:
As I see it, with these methods i can only search for full strings. My
problem is, that i get a string and my search parameter is only a part
of this string (so find, include, find_index etc doesn't work...)

You can use find/select with a regular expression:


irb(main):037:0> a =3D %w{test test2 nothing something}
=3D> ["test", "test2", "nothing", "something"]
irb(main):038:0> a.find {|x| /test/ =3D~ x}
=3D> "test"
irb(main):039:0> a.select {|x| /test/ =3D~ x}
=3D> ["test", "test2"]
irb(main):040:0> a.select {|x| /thing/ =3D~ x}
=3D> ["nothing", "something"]

The last one is even simpler with #grep:

irb(main):001:0> a =3D %w{test test2 nothing something}
=3D> ["test", "test2", "nothing", "something"]
irb(main):002:0> a.grep /thing/
=3D> ["nothing", "something"]
irb(main):003:0>

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
T

Thiel Chang

Hi Benjamin,

I don't want to be negative but I wonder if you have ever studied
seriously one of the many Ruby books which could answer your question.

Such a basic question should not be asked in this forum. Please do
your homework first.

Thiel

Op 7-2-2011 15:13, Benjamin S. schreef:
 
T

Thiel Chang

Hi Clent,

I agree this is a learning area and I apolize to be negative to Ben. Of
course, I could give him the solution but my remark should stimulate him
to study the Ruby books. Its better and more enjoyable to find the
solution of a (easy) programming problem yourself. On the other hand if
you don't have the time or are in a hurry to get on with your program
it's ok to use this forum.

Thiel

Op 7-2-2011 19:55, Clent Crumley schreef:
Thiel. You could've given him a link in the same amount of time you spent drafting that email. This is a learning area dude. Either help, or don't reply.

Ben, take a look here: http://www.ruby-forum.com/topic/168402




-----Original Message-----
From: Thiel Chang [mailto:[email protected]]
Sent: Monday, February 07, 2011 12:51 PM
To: ruby-talk ML
Subject: Re: Find in Array

Hi Benjamin,

I don't want to be negative but I wonder if you have ever studied
seriously one of the many Ruby books which could answer your question.

Such a basic question should not be asked in this forum. Please do
your homework first.

Thiel

Op 7-2-2011 15:13, Benjamin S. schreef:
Hey everbody

I searched like for 2 hours now and I didn't find anything. Can anyone
tell me, how i search an array? I got an array of strings and have to
search these. How do I manage to do this? Thx
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top