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

···

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

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

Stefano

···

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

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

···

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

D'Oh! ok, thx

···

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

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

···

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

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

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

···

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

--
Alex Gutteridge

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.

···

On Mon, Feb 7, 2011 at 3:22 PM, Benjamin S. <benjamin.stauffacher@gmail.com> 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...)

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

···

-----Original Message-----
From: Thiel Chang [mailto:schang@wxs.nl]
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

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

···

2011/2/7 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Mon, Feb 7, 2011 at 3:22 PM, Benjamin S. > <benjamin.stauffacher@gmail.com> 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"]

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

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

-----Original Message-----
From: Thiel Chang [mailto:schang@wxs.nl]
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