Find index of first non zeo value in array

with :
array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0]

I wrote :
array.index(array.detect {|x| x > 0}) => 15

is there a better and simpler way to do it ?
thanks

joss

Le dimanche 26 novembre 2006 15:00, Josselin a écrit :

joss
In that case, it is simpler to use an external counter, i think :

c = 0
array.each{|v| break if not v.zero?; c += 1}
puts c # => 15

On Nov 26, 2006, at 8:54 AM, Olivier wrote:

joss

In that case, it is simpler to use an external counter, i think :

c = 0
array.each{|v| break if not v.zero?; c += 1}
puts c # => 15

You can ask Ruby to maintain the counter, if you want:

require “enumerator”
=> true

array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0,
0, 0,
?> 0, 0, 0, 0, 0, 0]
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0]

result = array.enum_with_index.find { |n, i| n.nonzero? }.last
rescue nil
=> 15

array.slice!(15, 1)
=> [21]

array
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0]

result = array.enum_with_index.find { |n, i| n.nonzero? }.last
rescue nil
=> nil

James Edward G. II

Hi,

I wrote :
array.index(array.detect {|x| x > 0}) => 15

is there a better and simpler way to do it ?
thanks

joss

How about this:

array.index((array-[0])[0])

Regards,

Park H.

On 26.11.2006 14:59, Josselin wrote:

with :
array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0]

I wrote :
array.index(array.detect {|x| x > 0}) => 15

is there a better and simpler way to do it ?
thanks

Did we have a solution with #inject already? In case we didn’t:

irb(main):001:0> require ‘enumerator’
=> true
irb(main):002:0> a=Array.new(10,0) << 666 << 0
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 666, 0]
irb(main):003:0> a.to_enum(:each_with_index).inject(nil) {|pp,(x,i)|
break i unless x == 0}
=> 10
irb(main):004:0> a[0…5]
=> [0, 0, 0, 0, 0, 0]
irb(main):005:0> a[0…5].to_enum(:each_with_index).inject(nil)
{|pp,(x,i)| break i unless x == 0}
=> nil

Kind regards

robert

Robert K. wrote:

Did we have a solution with #inject already? In case we didn’t:

irb(main):001:0> require ‘enumerator’
=> true
irb(main):002:0> a=Array.new(10,0) << 666 << 0
=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 666, 0]
irb(main):003:0> a.to_enum(:each_with_index).inject(nil) {|pp,(x,i)|
break i unless x == 0}
=> 10

sub ‘inject(nil) {|pp,(x,i)|’, ‘find {|x,i|’

But I vote for adding to Enumerable or Array. #index_where or #find_by
or something. (My preference over alias-and-delegate.)

Devin

On Sunday 26 November 2006 21:22, Park H. wrote:

0, 0, 0, 0, 0]

array.index((array-[0])[0])

Regards,

Park H.


Don’t just search. Find. Check out the new MSN Search!
http://search.msn.com/

Not a generic way, but pretty good for this specific problem.

Regards,
Pradeep

Robert K. wrote:

Did we have a solution with #inject already?

array.inject(0){|i,e| if e>0; break i else i+1 end}

On 11/27/06, William J. [email protected] wrote:

is there a better and simpler way to do it ?
thanks

Did we have a solution with #inject already?

array.inject(0){|i,e| if e>0; break i else i+1 end}

array.inject(0){|i,e| break i if e>0; i+1 }

Using a regular expression …

array.join =~ /[^0]/

The inject method is faster, though, because you do not have to create
a string. I’m assuming that the “array.index((array-[0])[0])” solution
suffers from the same problem – creating another object under the
covers …

$ ruby tmp.rb
user system total real
inject 2.874000 0.000000 2.874000 ( 2.884000)
index 0.721000 0.000000 0.721000 ( 0.731000)
regexp 3.755000 0.000000 3.755000 ( 3.765000)

Wow! Looks like the “index” method wins hands down.

But mine is still shorter :wink:

TwP

William J. wrote:

thanks

Did we have a solution with #inject already?

array.inject(0){|i,e| if e>0; break i else i+1 end}

array.inject(0){|i,e| break i if e>0; i+1 }

uber_nasty_for_laughs.rb

array.to_s.split(/[1-9]/)[0].length

Just saw this…

@ ruby conf, there were talks about the speed of ruby’s regex

Being that ruby was the first language I picked up, I’m curious as to
how other languages(such as perl) compare from a performance
standpoint with your examples.

I’m not expecting perl to be slower an any scenerio, but is the perl
index() quicker or slower than a perl regex? If the latter, is the
performance ratio proportional to that of our ruby examples?