Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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 studying the Monastery: (2)
As of 2026-01-14 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (118 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.