Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

getting iteration number inside iteration loop

by jesuashok (Curate)
on Jun 29, 2007 at 07:20 UTC ( #624052=perlquestion: print w/replies, xml ) Need Help??

jesuashok has asked for the wisdom of the Perl Monks concerning the following question:


Apologies if this has been discussed already.

my $iteration_number = 1; foreach my $val ( @array ) { print ":$iteration_number:\n"; $iteration_number ++; }
In the above code to get the iteration number I am using a explicit flag to get the iteration count. I am interested to know, is there any built-in variable available to hold the iteration count ? For example $. holds the line number during the any FILEHANDLING operation.

Replies are listed 'Best First'.
Re: getting iteration number inside iteration loop
by blazar (Canon) on Jun 29, 2007 at 07:36 UTC

    Yes, this has been discussed ad nauseam and no, there's not such a variable. Hence one should either maintain a counter like you did or, when the actual list happens to be an array as in your example, despite the appeal of iterating over an @array with aliasing to its elements, just iterate over 0..$#array and use subscripting.

Re: getting iteration number inside iteration loop
by GrandFather (Sage) on Jun 29, 2007 at 07:34 UTC

    There is not. BTW, if you:

    my $iteration_number = 0; foreach my $val ( @array ) { $iteration_number ++; print ":$iteration_number:\n"; }

    then you know how may times the loop was entered by inspecting $iteration_number after the loop has completed.

    DWIM is Perl's answer to Gödel
Re: getting iteration number inside iteration loop
by ikegami (Pope) on Jun 29, 2007 at 13:19 UTC
    You probably want
    for my $i ( 0 .. $#array ) { print "$i: $array[$i]\n"; }

    Note that a for/foreach loop that loops over x..y is very effecient. x..y doesn't get flattened into a list as it would elsewhere.

Re: getting iteration number inside iteration loop
by ferreira (Chaplain) on Jun 29, 2007 at 14:47 UTC

    You may get this behavior with List::MoreUtils. The following presents two such solutions. They are not very pretty, but they work.

    First, you may generate an index array with [ 0 .. $#array ] and use each_arrayref/each_array to produce an iterator that loops over possibly many arrays at a time.

    use List::MoreUtils qw( each_arrayref ); my @a = qw( a b c d e ); my $each = each_arrayref( [ 0..$#a ], \@a ); while ( my ($idx, $v) = $each->() ) { print "$idx: $v\n"; }
    Second, you may use the documented behavior that the iterator returns the current index when given arguments ('index').
    use List::MoreUtils qw( each_array ); my @a = qw( a b c d e ); my $each = each_array( @a ); while ( my $v = $each->() ) { my $idx = $each->('index'); print "$idx: $v\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624052]
Approved by GrandFather
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2020-05-28 13:10 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (165 votes). Check out past polls.