in reply to Keeping a Count in foreach
If you don't want your counter to stick around after you're done looping through you list, use a bare block to limit the scope to your loop plus the counter declaration.
#! /usr/bin/perl use strict ; my @s = qw/ foo bar foobar FOO BAR FOOBAR / ; { my $count = 0 ; foreach my $e ( @s ) { print $count++, ": $e\n" ; } } # $count is no longer in scope.
|
---|