Jeff Schwab
6/28/2006 6:14:00 PM
Jeffrey Schwab wrote:
> aidy wrote:
>> Hi,
>>
>> I 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?
>
> You can give grep a block directly:
>
> a = [ "", "95424", "Joe Stalin", "Field Sales",
> "80%", "27/06/06", "", "Amend" ]
>
> re = /Stalin/, /Field Sales/, /80%/
>
> re.each do |r|
> a.grep(r) do |m|
> puts m
> end
> end
Or, using Kenosis' idea:
a = [ "", "95424", "Joe Stalin", "Field Sales",
"80%", "27/06/06", "", "Amend" ]
re = /Stalin|Field Sales|80%/
a.grep(re) do |m|
puts m
end