six24hourdays wrote:
Hello,

Hello,

I would like to know how to test for the last element of a list during
a foreach loop, e.g.

foreach $element (@List) {
    if (this is the last element) {
        do something
    }
}

What would I use for the IF condition?

You can't because a list does not have a name.

perldoc -q "What is the difference between a list and an array"


You can however do it with an array by comparing references:

$ perl -le'
my @x = "a" .. "z";
for my $elem ( @x ) {
    print $elem if \$elem == \$x[-1];
}
'
z



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to