Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

any mechanism to go to the end of a loop but do not exit the loop

by simonz (Sexton)
on Apr 17, 2014 at 10:38 UTC ( [id://1082625]=perlquestion: print w/replies, xml ) Need Help??

simonz has asked for the wisdom of the Perl Monks concerning the following question:

Hi
Is there any variable or keywords in Perl which will take me to the end of a loop but won't exit the loop.
Likewise , we have 'next' and 'last' in perl to go to the next iteration ignoring the statements underneath and exiting the loop respectively.
Is there any such mechanism for going to the end of the loop ?

  • Comment on any mechanism to go to the end of a loop but do not exit the loop

Replies are listed 'Best First'.
Re: any mechanism to go to the end of a loop but do not exit the loop
by bigj (Monk) on Apr 17, 2014 at 10:47 UTC
    You can always use a goto statement, like
    while (foo()) { ... goto JUST_BEFORE_END_OF_LOOP if bar(); ... ... JUST_BEFORE_OF_LOOP: clean_up }
    You might also checkout the continue flow control. With it, you could write it like:
    while (foo()) { ... next if bar(); ... ... } continue { clean_up }

    Greetings,
    Janek Schleicher

      thanks for understanding my requirement and answering accordingly.
        Maybe you should say explicitly what this requirement is. Although there is at least one other programming language that I use commonly in which I am using the goto statement quite commonly (but only forward goto's, not spaghetti goto's, for those that followed the debate on the subject between, say, Dijstra, Knuth and Wirth, to name just a few) in the late 1960s and early 1970's), I don't see any reason to use this kind of goto's in Perl.

        And Larry Wall also said that he never needed to use this form of goto in Perl. (There is in Perl another form of goto (the "goto &name" version) which is very useful for some advanced techniques, but that's a different story and this is not the subject now.)

        But as a simple flow control statement, I am very skeptical. IMHO, Perl offers all the flow control statements that are needed for a good structured programming, the goto statement should probably not be used at all (except for the special goto coderef version mentioned above).

Re: any mechanism to go to the end of a loop but do not exit the loop
by Bloodnok (Vicar) on Apr 17, 2014 at 10:48 UTC
    Strictly speaking, last doesn't go to the next iteration, it exists the iteration at that point. Is it possible that you're thinking of the continue block?

    A user level that continues to overstate my experience :-))
Re: any mechanism to go to the end of a loop but do not exit the loop
by Laurent_R (Canon) on Apr 17, 2014 at 10:50 UTC
    That is what next or last do in effect: they bring you both to the end of the loop and either start into the loop again or exit the loop. Or do you have something else in mind? You might also take a look at the continue statement.

    Edit: oops, many answers while I was typing, sorry for just essentially saying the same thing as others.

Re: any mechanism to go to the end of a loop but do not exit the loop
by ww (Archbishop) on Apr 17, 2014 at 11:45 UTC
    Sorry, my reading of your request is:
    1. do some stuff in the loop, n times
    2. When some condition is true, go to end of loop...
            that is, to the zero-length&height line between the last instruction and the (whatever) outside the loop
    3. and just sit there... interminably.

    That effectively halts the code and maybe the computer... and certainly halts my speculation about what you want to do, unless you mean to execute your spec as some sort of malware or prank.



    Quis custodiet ipsos custodes. Juvenal, Satires

    check Ln42!

Re: any mechanism to go to the end of a loop but do not exit the loop
by lidden (Curate) on Apr 17, 2014 at 10:44 UTC
    How should this be different from next?

    Edit: Or last depending on what you meant.

Re: any mechanism to go to the end of a loop but do not exit the loop
by fishmonger (Chaplain) on Apr 17, 2014 at 15:05 UTC

    Sounds like you have an XY problem.

    Can you tell us about the problem you're trying to solve rather than asking about how to implement your chosen solution to that problem you haven't described?

Re: any mechanism to go to the end of a loop but do not exit the loop
by GotToBTru (Prior) on Apr 17, 2014 at 14:02 UTC

    The OP indicates he knows what next does, so I assume the reason to jump to the end of the loop is in order to do something there. I also assumed that the last thing should not be done unless we're skipping to the end. Here is my suggestion:

    for ($i = 0; $i < 4; $i++) { print "Start $i "; if ($i == 2) { goto TWO; } print "End\n"; next; TWO: print "Middle\n"; }

    Output:

    Start 0 End Start 1 End Start 2 Middle Start 3 End
Re: any mechanism to go to the end of a loop but do not exit the loop
by AnomalousMonk (Archbishop) on Apr 17, 2014 at 16:35 UTC
    ... mechanism for going to the end of the loop ?

    ... and then doing what? Like most other respondents to the OP, I'm left with that question in my mind.

    Be that as it may, you might also check out redo.

Re: any mechanism to go to the end of a loop but do not exit the loop
by fishy (Friar) on Apr 18, 2014 at 11:34 UTC
    Do you mean this:
    my $r = 0; while ($r < 10) { print "one iteration\n"; $r++; } continue { $r = 10; print "I'm at the end but still don't exited the loop\n"; }
Re: any mechanism to go to the end of a loop but do not exit the loop
by wjw (Priest) on Apr 19, 2014 at 21:19 UTC
    As others have mentioned, your question is only partly formed. To help in any meaningful way, one would need to know
    1. under what condition do you want to skip the loop activities?
    2. what do you want to do when you bypass the contents of the loop(where do you want to go)?
    3. once you have gone to where you want to go after #2 above, what do you want to do when you come back?
    I am going to guess that you want to continue working inside the loop(from question #3 because going to the end and doing nothing is not sane)
    I am going to restate what I think you are asking here:

    enter a loop
    do what is in loop unless/until some condition(if/unless) is met
    **do something else(call a sub?)
    then come back and continue to the next iteration of the loop(next)

    It seems to me that this all boils down to putting a next/(some conditional) in the appropriate place in the loop activities.

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...
Re: any mechanism to go to the end of a loop but do not exit the loop
by fishy (Friar) on Apr 18, 2014 at 17:55 UTC
    or maybe this:
    my $r = "ju"; while (1) { print uc $r if $r and $r ne "ju"; # do some processing $r and next; # I'm now at the end of the loop print uc "hacker", "\n"; # adding missing word # WARNING: uncomment the following line or push the button #not $r and last; } continue { $r = <DATA>; } __DATA__ Just Another Perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found