From: Mike Stok Date: 2008-11-12T08:03:46+09:00 Subject: Re: Advanced arrays On Nov 11, 2008, at 5:29 PM, Stuart Clarke wrote: > Hi all, > > I have a quick brain teaser. I have an array full of data broken down > into indivdual entries. I running a simple if statement to check for > an > entry in the array, if my if statement finds that entry its prints out > that entry. I want to add an extra step into this so, when my if > statement finds an entry, I want it to look at the next entry and > check > for something else, if it finds that, I want to look at the next entry > and check for something else, if it finds all of the three entries > in a > row, it prints out "You have found what you want" > > Any ideas? It might be a case where Enumerable.each_cons will do what you want. Not knowing the size of the data set, and not having benchmarked it, this does seem to work: #!/usr/bin/env ruby def search_array(array, look_for) array.each_cons(look_for.length) do |chunk| if chunk == look_for puts "You have found what you want" return end end puts "no luck..." end sample = ('a' .. 'z').to_a search_array(sample, ['l', 'm', 'n']) search_array(sample, ['l', 'p', 'n']) Hope this helps, Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.