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

How can you go back to the top of a for loop? I tried using next; and it didn't seem to make any difference, it continued going down the script as usual. Then I tried LAST and, well, that made the loop completely stop.

Replies are listed 'Best First'.
Re: go back to top of for loop
by bobf (Monsignor) on Jun 03, 2006 at 19:10 UTC

    As others have mentioned, without some more information we can't give you help that is specific to your problem. As a general pointer, though (and in an attempt to teach you how to fish), please see the section entitled "Loop Control" in perlsyn and the individual functions in perlfunc. There are three basic commands:

    • next: starts the next iteration of the loop (the continue block, if any, is executed)
    • last: immediately exits the loop (the continue block, if any, is not executed)
    • redo: restarts the loop block without evaluating the conditional again (the continue block, if any, is not executed)

    Please note that there are situations in which the next, last, and redo commands cannot be used (see perlfunc).

    HTH

Re: go back to top of for loop
by liverpole (Monsignor) on Jun 03, 2006 at 17:49 UTC
    Is something like this what you're looking for ...?
    use strict; use warnings; TOP: for (my $i = 1; $i < 10; $i++) { if (5 == $i) { next TOP; } print "Iteration $i\n"; }

    It will, of course, print all the integers from 1 to 9 except for the number 5.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: go back to top of for loop
by Limbic~Region (Chancellor) on Jun 03, 2006 at 17:53 UTC
    Anonymous Monk,
    I think you want redo but a better explanation on your part might be necessary if I'm wrong.

    Cheers - L~R

Re: go back to top of for loop
by GrandFather (Saint) on Jun 03, 2006 at 18:25 UTC

    Same ol, same ol - show us some code, show us what you expect, show us what you get. There is even less here for mind reading than usual; I suspect you are not very strong in the force, but we can help with that if you do the exercises.


    DWIM is Perl's answer to Gödel