From: dblack@... Date: 2006-11-27T00:23:09+09:00 Subject: Re: find index of first non zeo value in array ---2049402039-511287211-1164554586=:11709 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-511287211-1164554586=:11709" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-511287211-1164554586=:11709 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sun, 26 Nov 2006, Olivier wrote: > Le dimanche 26 novembre 2006 15:00, Josselin a =E9crit=A0: >> with : >> array =3D [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}) =3D> 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 =3D 0 > array.each{|v| break if not v.zero?; c +=3D 1} > puts c # =3D> 15 Or: c =3D array.each_with_index {|e,i| break i unless e.zero? } David --=20 David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org ---2049402039-511287211-1164554586=:11709-- ---2049402039-511287211-1164554586=:11709--