Yet another variant:
my @elems = qw(a b c d e); my $n = $#elems; for my $elem (@elems) { print "last elem = $elem\n" if !$n--; }
Update: as to your updated question: if you want to avoid creating a named array at any price, you could do something like this (but I'm not sure if I should seriously recommend that...)
my $n; for my $elem (map {$n++; $_} qw(a b c d e)) { print "last elem = $elem\n" if !--$n; }
In reply to Re: Test for the last element in a foreach
by almut
in thread Test for the last element in a foreach
by tdevil76
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |