# Grep-ing an array

**URL:** <https://www.ruby-forum.com/t/grep-ing-an-array/96439>\
**Category:** Ruby\
**Created:** [June 28, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/grep-ing-an-array/96439 "2006-06-28T20:00:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [June 28, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/grep-ing-an-array/96439/1 "2006-06-28T20:00:59Z")

</div>

Hi,

I am trying to find whether specific strings are contained within a  
array

table\_array contains these elements [["", “95424”, “Joe Stalin”, “Field  
Sales”, “80%”, “27/06/06”, “”, “Amend”]]

I am grep-ing whether the text I am looking for is in. So I have got an  
array of regex

reg\_text = /Stalin/, /Field Sales/, /80%/

Now, if the grep finds the string pattern a full string will be  
returned  
if x.grep(y) == /Stalin/  
=\> [“Joe Stalin”]

so, I have got another array

real\_text = [“Joe Stalin”], [“Field Sales”], [“80%”]

this is the code

```
$table_array.each{|x|
  reg_text.each{|y|
    real_text.each{|z|
      if x.grep(y) == z then p "#{z} is in table" end
    }
   }
      }

```

But I think it is messy, and I wonder if there was a better way?

Cheers

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [June 28, 2006, 8:37pm UTC](https://www.ruby-forum.com/t/grep-ing-an-array/96439/2 "2006-06-28T20:37:51Z")

</div>

Quoting aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

What about Array.assoc(“mystring”)?

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [June 28, 2006, 9:51pm UTC](https://www.ruby-forum.com/t/grep-ing-an-array/96439/3 "2006-06-28T21:51:46Z")

</div>

how’s that?!

a = ["", “95424”, “Joe Stalin”, “Field Sales”, “80%”, “27/06/06”, “”,  
“Amend”]

puts a.select {|i| i =~ /(Stalin|Field Sales|80%)/ }

Pau Garcia i Quiles schrieb:
