Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Test for the last element in a foreach

by ikegami (Patriarch)
on Aug 02, 2007 at 20:14 UTC ( [id://630383]=note: print w/replies, xml ) Need Help??


in reply to Test for the last element in a foreach

In the CB, I presented:

for my $i (0..$#array) { for ($array[$i]) { ... if ($i == $#array) { ... } ... } }

Using this method, no change will be required to the body of your loop. $_ is still aliased to the array element as it normally is in a foreach loop. Changes to it will be reflected in the array.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2025-03-26 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (68 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.