Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Test for the last element in a foreach

by almut (Canon)
on Aug 02, 2007 at 20:21 UTC ( [id://630387]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my @elems = qw(a b c d e);
    
    ...
    for my $elem (@elems) {
        print "last elem = $elem\n" if !$n--;
    }
    
  2. or download this
    my $n;
    for my $elem (map {$n++; $_} qw(a b c d e)) {
        print "last elem = $elem\n" if !--$n;
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://630387]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2025-03-27 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (69 votes). Check out past polls.

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.