my @array = qw(red blue green yellow black white purple brown orange gray);
foreach (@array) {
do_something_with($_);
}
####
# Inputs: $1 ... color name
# $2 ... The color index (a new arg)
##
sub do_something_with {
my ($color, $idx) = @_;
# For now, just print the color prefixed with its index...
printf " %2d. '$color'\n", $idx;
}
####
for (my $i = 0; $i < @array; $i++) {
my $item = $array[$i];
do_something_with($item, $i);
}
####
my $i;
foreach (@array) {
do_something_with($item, $i++);
}
####
use feature ":5.10";
my @array = qw(red blue green yellow black white purple brown orange gray);
foreach (@array) {
do_something_with($item, state i++);
}
# Prints
# 0. 'red'
# 1. 'blue'
# 2. 'green'
# 3. 'yellow'
# 4. 'black'
# 5. 'white'
# 6. 'purple'
# 7. 'brown'
# 8. 'orange'
# 9. 'gray'
####
for (@array) {
do_something_with($_, ++ state $i);
}
# Prints
# 1. 'red'
# 2. 'blue'
# 3. 'green'
# 4. 'yellow'
# 5. 'black'
# 6. 'white'
# 7. 'purple'
# 8. 'brown'
# 9. 'orange'
# 10. 'gray'
####
map { do_something_with($_, state $i++) } @array;