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 wrote:
> On 08/09/2013 04:34 AM, Jing Yu wrote:
>> You probably can use 'state' instead of 'my' to keep $counter
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
las
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
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 wrote:
> my $counter = 0;
> foreach my $e ( a .. z ) {
> $counter++;
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 ha