[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Advanced arrays

Stuart Clarke

11/11/2008 10:29:00 PM

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
--
Posted via http://www.ruby-....

6 Answers

Todd Benson

11/11/2008 10:45:00 PM

0

On Tue, Nov 11, 2008 at 4:29 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> 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?

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

Todd

Rob Biedenharn

11/11/2008 10:59:00 PM

0

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

> On Tue, Nov 11, 2008 at 4:29 PM, Stuart Clarke
> <stuart.clarke1986@gmail.com> 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?
>
> 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 Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Mike Stok

11/11/2008 11:04:00 PM

0


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 <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





brabuhr

11/11/2008 11:05:00 PM

0

On Tue, Nov 11, 2008 at 5:29 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> 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)

Todd Benson

11/12/2008

0

On Tue, Nov 11, 2008 at 4:47 PM, Todd Benson <caduceass@gmail.com> wrote:
> On Tue, Nov 11, 2008 at 4:29 PM, Stuart Clarke
> <stuart.clarke1986@gmail.com> 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?
>
> 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

David A. Black

11/12/2008 3:39:00 AM

0

Hi --

On Wed, 12 Nov 2008, Mike Stok wrote:

>
> 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'])

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!