counting .each

Discussion in 'Ruby' started by Mk 27, Jun 2, 2009.

  1. Mk 27

    Mk 27 Guest

    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/.
    Mk 27, Jun 2, 2009
    #1
    1. Advertising

  2. On Mon, Jun 1, 2009 at 7:06 PM, Mk 27 <> 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/person/9021-rick-denatale
    LinkedIn: http://www.linkedin.com/in/rickdenatale
    Rick DeNatale, Jun 2, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    1
    Views:
    396
    Eliyahu Goldin
    Aug 3, 2005
  2. Tjerk Wolterink

    xsl:for-each for each 3 elements problem

    Tjerk Wolterink, Nov 3, 2004, in forum: XML
    Replies:
    3
    Views:
    416
    Tjerk Wolterink
    Nov 3, 2004
  3. sophie_newbie

    Counting number of each item in a list.

    sophie_newbie, Mar 19, 2006, in forum: Python
    Replies:
    16
    Views:
    399
    Peter Otten
    Mar 20, 2006
  4. Pat Maddox
    Replies:
    6
    Views:
    141
    Marcin Mielżyński
    Jan 20, 2006
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    185
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page