# Best way to search through an array for an item matching a s

**URL:** <https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369>\
**Category:** Ruby\
**Created:** [November 23, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369 "2009-11-23T17:36:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 23, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/1 "2009-11-23T17:36:08Z")

</div>

Hi everyone,

am trying to find a nice easy/simple way to

- search through an array of strings match the string I’ve input
- use that index to print out the word before in the array.

for example  
[ahoy there,hello,me hearties,dear friends]

‘hello’ should output ‘ahoy there’

[http://pastie.org/711384](http://pastie.org/711384)

I’m hoping there’s a way of doing this without regexp…

Thanks very much,

Dan

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 23, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/2 "2009-11-23T17:51:07Z")

</div>

Daniel Webb wrote:

> am trying to find a nice easy/simple way to
> 
> - search through an array of strings match the string I’ve input
> - use that index to print out the word before in the array.
> 
> for example  
> [ahoy there,hello,me hearties,dear friends]
> 
> ‘hello’ should output ‘ahoy there’

[http://ruby-doc.org/core/classes/Array.html#M002178](http://ruby-doc.org/core/classes/Array.html#M002178)

def print\_element\_before\_x\_in\_y x, y # Shut up. I am GOOD at method  
names.  
y.index x  
x.nil? ? nil : y[x-1]  
end

This, minds you, wraps around, so if you find the first item in the  
array, it’ll return y[-1] … 😉

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 23, 2009, 5:54pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/3 "2009-11-23T17:54:15Z")

</div>

Daniel Webb:

> am trying to find a nice easy/simple way to
> 
> - search through an array of strings match the string I’ve input
> - use that index to print out the word before in the array.

> for example  
> [ahoy there,hello,me hearties,dear friends]

> ‘hello’ should output ‘ahoy there’

dict = [‘ahoy there’, ‘hello’, ‘me hearties’, ‘dear friends’]  
dict[dict.index(‘hello’) - 1] # =\> ‘ahoy there’

â€” Shot

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 23, 2009, 5:56pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/4 "2009-11-23T17:56:55Z")

</div>

Daniel Webb wrote:

> Hi everyone,
> 
> am trying to find a nice easy/simple way to
> 
> - search through an array of strings match the string I’ve input
> - use that index to print out the word before in the array.
> 
> for example  
> [ahoy there,hello,me hearties,dear friends]
> 
> ‘hello’ should output ‘ahoy there’
> 
> [http://pastie.org/711384](http://pastie.org/711384)
> 
> I’m hoping there’s a way of doing this without regexp…
> 
> Thanks very much,
> 
> Dan

ary = [“hello”, “hey”, “hi”, “hola”]  
ary[ary.index(“hey”) - 1] #=\> “hello”  
ary[ary.index(“hello”) - 1] #=\> “hola”  
ary.index(“sup”) #=\> nil

Maybe that will give ya a bit of a boost!

~Jeremy

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 24, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/5 "2009-11-24T12:55:43Z")

</div>

Thanks Jeremy. That works a treat, however if I’m using an array  
populated by values from a text file I get:

undefined method `-’ for nil:NilClass (NoMethodError)

I’ve tried flattening the array but it didn’t make any difference (as  
that’s caused issues in the past), but to it’s no use.  
Using IRB I can see it’s putting in new line characters when it’s  
stripping the line.

i.e. “arrrrrrgh!\n”

I presume this is what is producing the error, does anyone know a way  
around this error?

Thanks very much,  
Dan

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 24, 2009, 3:57pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/6 "2009-11-24T15:57:25Z")

</div>

Daniel Webb wrote:

> Thanks Jeremy. That works a treat, however if I’m using an array  
> populated by values from a text file I get:
> 
> undefined method `-’ for nil:NilClass (NoMethodError)

Because it’s not finding the string, so the index is nil, and you can’t  
subtract from nil.

> I’ve tried flattening the array but it didn’t make any difference (as  
> that’s caused issues in the past),

That’s cargo-cult programming. You’ve got to understand your errors,  
not take wild shots in the dark.

> but to it’s no use.  
> Using IRB I can see it’s putting in new line characters when it’s  
> stripping the line.
> 
> i.e. “arrrrrrgh!\n”
> 
> I presume this is what is producing the error, does anyone know a way  
> around this error?

Well, think about it. You can’t find “string” because the array  
contains “string\n”. That suggests that you want to remove the \n from  
the array. How do you do that? (Hint: it’s a one-liner.)

> Thanks very much,  
> Dan

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 23, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/7 "2009-11-23T17:51:42Z")

</div>

Aldric G. wrote:

> def print\_element\_before\_x\_in\_y x, y # Shut up. I am GOOD at method  
> names.  
> index = y.index x  
> index.nil? ? nil : y[x-1]  
> end

What? I proofread my code.

---

<div class="post-metadata">

**Author:** ![dewie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@dewie](https://www.ruby-forum.com/u/dewie)\
**Post date:** [November 24, 2009, 6:34pm UTC](https://www.ruby-forum.com/t/best-way-to-search-through-an-array-for-an-item-matching-a-s/179369/8 "2009-11-24T18:34:51Z")

</div>

Thanks I need to just stare at that API for a lot longer…

“.chomp”

Thank you very much
