[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

counting .each

MK

6/1/2009 11:06:00 PM

Is there some equivalent to file.lineno when using array.each? Eg, if I
am iterating thru an array of strings searching for a pattern, and I
want to save the current array index, do I have to do this:

count = 0
array.each { |e|
finds.push count if e =~ /pattern/
count += 1
}

or is there a "array.currentindex"? I guess I could use each_index...
--
Posted via http://www.ruby-....

1 Answer

Rick DeNatale

6/1/2009 11:58:00 PM

0

On Mon, Jun 1, 2009 at 7:06 PM, Mk 27 <halfcountplus@intergate.com> wrote:
> Is there some equivalent to file.lineno when using array.each? =A0Eg, if =
I
> am iterating thru an array of strings searching for a pattern, and I
> want to save the current array index, do I have to do this:
>
> count =3D 0
> array.each { |e|
> =A0 =A0finds.push count if e =3D~ /pattern/
> =A0 =A0count +=3D 1
> }
>
> or is there a "array.currentindex"? =A0I guess I could use each_index...

array.each_with_index do | e, count|
finds.push count if e =3D~ /pattern/
end
--=20
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...