in reply to foreach only for a section of a list
Here are two other ideas: you can store and print the previous value each iteration or you can pop the final element off the array then use foreach on the remaining elements.
use strict; use warnings; my @somewords = qw(a b c d); my $previous_core = ''; foreach my $core (@somewords) { print "$previous_core\n" if $previous_core ne ''; $previous_core=$core; } print "************\n"; my $popword = pop @somewords; print "popped >${popword}<\n"; print "\@somewords:\n"; print "$_\n" foreach @somewords; print "************\n";
|
|---|