[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

find index of first non zeo value in array

Josselin

11/26/2006 2:00:00 PM

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

22 Answers

Olivier

11/26/2006 2:55:00 PM

0

Le dimanche 26 novembre 2006 15:00, Josselin a écrit :
> 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

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

dblack

11/26/2006 3:23:00 PM

0

dblack

11/26/2006 3:26:00 PM

0

James Gray

11/26/2006 3:40:00 PM

0

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

> Le dimanche 26 novembre 2006 15:00, Josselin a écrit :
>> 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
>
> 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 Gray II

Park Heesob

11/26/2006 3:52:00 PM

0


Hi,

>From: Josselin <josselin@wanadoo.fr>
>Reply-To: ruby-talk@ruby-lang.org
>To: ruby-talk@ruby-lang.org (ruby-talk ML)
>Subject: find index of first non zeo value in array
>Date: Sun, 26 Nov 2006 23:00:10 +0900
>
>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
>
>
How about this:

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

Regards,

Park Heesob

_________________________________________________________________
Don't just search. Find. Check out the new MSN Search!
http://searc...


dblack

11/26/2006 3:53:00 PM

0

dblack

11/26/2006 4:09:00 PM

0

Devin Mullins

11/26/2006 4:30:00 PM

0

James Edward Gray II wrote:
> I believe there has been talk in the past of having index() take a
> block for matching.
Meh... talk schmalk...

class Array
alias orig_index index
def index(*args)
return orig_index(*args) unless block_given?
(0...length).each do |i|
return i if yield self[i]
end
nil
end
end

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]
array.index 21 #=> 15
array.index {|n| n.nonzero? } #=> 15

Devin


Ross Bamford

11/26/2006 5:28:00 PM

0

On Mon, 2006-11-27 at 01:00 +0900, James Edward Gray II wrote:

> I believe there has been talk in the past of having index() take a
> block for matching. That would solve this problem ideally. I can
> submit an RCR if people think it's worth it, but I'm pretty sure Matz
> said it was OK last time it came up... (Correct me if I am wrong!)

IIRC Matz accepted it straight into 1.9. It's implemented there now:

$ ruby9 -ve 'p [0,0,0,15,0].index { |e| e > 0 }'
ruby 1.9.0 (2006-11-26 patchlevel 0) [i686-linux]
3

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk


Martin DeMello

11/26/2006 5:40:00 PM

0

On 11/26/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
>
> This exchange relates to something I've been pondering for a while,
> namely: is there always (or very, very often) an inverse relation
> between elegance of code and efficiency? I don't mean to sound like
> I'm singling out your example -- on the contrary, it seems that over
> and over we see cases where a nice concise solution bombs out compared
> to one that's longer, possibly less clear (I still can't read the
> enum* stuff as quickly and confidently as I can read the regular
> Enumerable stuff, though that may just be due to stupidity), but
> faster.

Seems to be a general symptom of everything being optimised to run C
code - the C-like solutions win out.

m.