Find in Array

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

On Monday 07 February 2011 23:13:28 Benjamin S. wrote:

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

Have you looked at the documentation for Enumerable#find and/or
Enumerable#grep?

Stefano

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…)

On Mon, 7 Feb 2011 23:22:20 +0900, Benjamin S. wrote:

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…)

irb(main):001:0> [“foo”,“bar”,“foobar”].find{|x| x.include?(“ooba”)}
=> “foobar”

On Mon, Feb 7, 2011 at 3:22 PM, Benjamin S.
[email protected] wrote:

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.

Having had a look at
http://ruby-doc.org/ruby-1.9/classes/Enumerable.html#M002710, you might
come up with something like
[‘a’, ‘list’, ‘of’, ‘strings’].select{ |string| string.include? ‘i’}

regards

2011/2/7 Jess Gabriel y Galn [email protected]:

=> [“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”]

The last one is even simpler with #grep:

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

Kind regards

robert

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:

D’Oh! ok, thx

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: Newbie to ruby, search an array - Ruby - Ruby-Forum