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.
In reply to Re: Keeping a Count in foreach
by DamnDirtyApe
in thread Keeping a Count in foreach
by arunhorne
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |