# Advanced arrays

**URL:** <https://www.ruby-forum.com/t/advanced-arrays/153083>\
**Category:** Ruby\
**Created:** [November 11, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/advanced-arrays/153083 "2008-11-11T23:31:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 11, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/1 "2008-11-11T23:31:44Z")

</div>

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?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 11, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/2 "2008-11-11T23:50:29Z")

</div>

On Tue, Nov 11, 2008 at 4:29 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any ideas?

Depends on the objects in the array, but you might try #join followed  
by #include?.

Todd

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 12, 2008, 12:02am UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/3 "2008-11-12T00:02:13Z")

</div>

On Nov 11, 2008, at 5:45 PM, Todd B. wrote:

> > that entry. I want to add an extra step into this so, when my if  
> > Depends on the objects in the array, but you might try #join followed  
> > by #include?.
> 
> Todd

See Enumerable#each\_cons

your\_array.each\_cons(3) do |entry, something, something\_else|  
puts “You have found what you want” if complex\_condition  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 12, 2008, 12:08am UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/4 "2008-11-12T00:08:05Z")

</div>

On Tue, Nov 11, 2008 at 5:29 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

Not as pretty as each\_cons, but:

def foo?(array, a, b, c)  
i = array.index(a)  
return false unless i  
if (array[i+1] == b) and (array[i+2] == c)  
puts “You have found what you want”  
return true  
end  
false  
end

p foo?([1,2,3,4,5,6], 3, 4, 5)  
p foo?([1,2,3,4,5,6], 3, 4, 6)

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 12, 2008, 12:06am UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/5 "2008-11-12T00:06:54Z")

</div>

On Nov 11, 2008, at 5:29 PM, Stuart C. wrote:

> 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 S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 12, 2008, 1:03am UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/6 "2008-11-12T01:03:43Z")

</div>

On Tue, Nov 11, 2008 at 4:47 PM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 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?
> 
> Depends on the objects in the array, but you might try #join followed  
> by #include?.

That may have been a bit unfair (and wrong too, because you might have  
nils in your array and temporarily destroys objects you don’t expect).

How about (doesn’t work if you have trailing nils in your search  
array)…

#this is just preemptive room cleaning, important code is further below  
m = my\_array.dup.unshift(Object.new) #big array  
g = good\_array #stuff I want to look for

#take one off the beginning until I match what I’m looking for  
m.shift until (m[0…2] == g || m.empty?)  
puts “Great!” if !m.empty?

…which makes a lot of english sense to me. The my\_array is #dup’ed  
because #shift is destructive in pulling each thing from the  
beginning. g is just there to be concise.

You can condense this, too, if you wanted. I don’t know if you need  
the #unshift or not.

cheers,  
Todd

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [November 12, 2008, 4:41am UTC](https://www.ruby-forum.com/t/advanced-arrays/153083/7 "2008-11-12T04:41:57Z")

</div>

Hi –

On Wed, 12 Nov 2008, Mike S. wrote:

> > for something else, if it finds that, I want to look at the next entry  
> > #!/usr/bin/env ruby
> 
> sample = (‘a’ … ‘z’).to\_a  
> search\_array(sample, [‘l’, ‘m’, ‘n’])  
> search\_array(sample, [‘l’, ‘p’, ‘n’])

You can also take advantage of the fact that each\_cons returns an  
enumerator when called without a block:

def search\_array(array, look\_for)  
if array.each\_cons(look\_for.length).include?(look\_for)  
puts “You have found what you want”  
else  
puts “no luck…”  
end  
end

(which may or may not be to everyone’s taste, but kind of interesting  
that it can be done this way).

David
