Re: Array iterator count

2013-08-09 Thread Dermot
my $counter = 0; foreach my $e ( a .. z ) { $counter++; if ( $counter == 5 ) { } } I know this is a perl idiom but I, and I suspect others, would find a perl variable useful for the keeping the count when iterating. The draw back with the above is that $counter

Re: Array iterator count

2013-08-09 Thread Jing Yu
You probably can use 'state' instead of 'my' to keep $counter in scope. foreach my $e ( 'a'..'z' ) { state $counter++; if ( $counter == 5 ) { say $e; } } Cheers, Jing On 9 Aug 2013, at 16:24, Dermot paik...@gmail.com wrote: my $counter = 0; foreach my $e ( a .. z ) {

Re: Array iterator count

2013-08-09 Thread Uri Guttman
On 08/09/2013 04:24 AM, Dermot wrote: my $counter = 0; foreach my $e ( a .. z ) { $counter++; if ( $counter == 5 ) { } } I know this is a perl idiom but I, and I suspect others, would find a perl variable useful for the keeping the count when iterating. The

Re: Array iterator count

2013-08-09 Thread Uri Guttman
On 08/09/2013 04:34 AM, Jing Yu wrote: You probably can use 'state' instead of 'my' to keep $counter in scope. foreach my $e ( 'a'..'z' ) { state $counter++; if ( $counter == 5 ) { say $e; } } and what if that code is run again in the same program? it will keep the

Re: Array iterator count

2013-08-09 Thread Jing Yu
That is true.. Perhaps it's better to introduce a bare block enclosing the loop, and declare $count as 'my' just before 'foreach'. Cheers, Jing On 9 Aug 2013, at 16:39, Uri Guttman u...@stemsystems.com wrote: On 08/09/2013 04:34 AM, Jing Yu wrote: You probably can use 'state' instead of