Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > counting .each

Reply
Thread Tools

counting .each

 
 
Mk 27
Guest
Posts: n/a
 
      06-01-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      06-01-2009
On Mon, Jun 1, 2009 at 7:06 PM, Mk 27 <(E-Mail Removed)> 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.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/pers...-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Counting bits Steve VHDL 7 05-12-2004 05:42 AM
Re: tired of counting.... UDP socket bug Feroze [MSFT] ASP .Net 0 01-19-2004 02:42 AM
Re: tired of counting.... UDP socket bug Willy Denoyette [MVP] ASP .Net 0 01-13-2004 08:57 PM
Re: tired of counting.... UDP socket bug Ray Cassick \(Home\) ASP .Net 3 01-13-2004 06:53 PM



Advertisments