From: 7stud -- Date: 2011-04-05T08:41:04+09:00 Subject: Re: Using grep on subarrays - help! Simon Harrison wrote in post #990886: > Thanks for all the tips. I think #select fits best: > > file_results = @films.select { |a, b| /#{@film}/i =~ a } > > > Just one question. Obviously in the above, a and b refer to the first > and second elements in each subarray. Let's say we have this array: > > => [["one", "two", "three"], ["one"], ["two", "three"], ["one", > "three"]] > Neither grep() or include?() depended on the size of the sub-arrays they are searching, so the answer is the same: data = [ ["one", "two", "three"], ["one"], ["two", "three"], ["one","three"] ] target = 'one' results = data.select do |arr| arr.include?(target) end p results --output:-- [["one", "two", "three"], ["one"], ["one", "three"]] However, if your target will only appear in the first position of the array, then it is much more efficient to just check the first element of each array: data = [ ["one", "two", "three"], ["ONE"], ["two", "three"], ["oNe","three"] ] target = 'one' results = data.select do |arr| arr[0].downcase == target end p results --output:-- [["one", "two", "three"], ["ONE"], ["oNe", "three"]] -- Posted via http://www.ruby-forum.com/.