Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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 ( #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? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2022-05-25 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?