Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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:

monks,

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.

Comment on getting iteration number inside iteration loop
Select or Download Code
Re: getting iteration number inside iteration loop
by GrandFather (Cardinal) 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 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 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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-08-01 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls