Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Perl's feature to determine, in current point of loop, that this is the last one?

by syphilis (Archbishop)
on Jan 23, 2022 at 04:51 UTC ( [id://11140738]=note: print w/replies, xml ) Need Help??


in reply to Perl's feature to determine, in current point of loop, that this is the last one?

...that this is the last of the loop there will be no next iteration ?

As shown in jwkrahn's example, you have to code it in yourself. (And this is usually quite possible.)

But perl itself will not know that it's in the last loop until that last loop has terminated and the looping condition is re-evaluated (and fails).
By that time the moment has passed, and all perl could tell you is "Oh ... that last loop I just did was the final loop".

If you're looking for a way to bail out of a loop before the loop condition has failed, you can always use last:
perl -le 'for(1..10){print $_; last if $_ == 5}' 1 2 3 4 5
Cheers,
Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found