use strict; my @array = qw( wilma fred barney betty ); my $ctr = 0; my $next_name; foreach my $name ( @array ) { print "$name is the current element.\t"; # Before using the next element, test for its existence. $next_name = exists $array[ $ctr + 1 ] ? $array[ $ctr + 1 ] : "unavailable" ; # Or you could break out immediately or perhaps have some # "end of array" code: # last if $next_name eq 'unavailable'; # or: # if ( $next_name eq 'unavailable' ) { # # do whatever you do when no more elements # } print "The next element is $next_name.\n"; } continue { $ctr ++; } # Use of the '} continue {' line is more fancy than necessary; # It's useful if you plan to break out of your loop with # a 'next' command.