Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
For the completeness of the "next" topic, we have to introduce the two brothers of "next": "last" and "redo".

  1. next

    The "next" statement allows you to jump to the next iteration of the loop without executing the remaining statements of the current iteration.
    foreach (1..10) { next if ($_ == 9); print("$_ "); }
    Here's what it looks like:

    1 2 3 4 5 6 7 8 10

    As you can see, 9 is missing - this is because when the value of $_ hits 9, Perl uses the "next" statement to skip to the next iteration of the loop, and so 9 never gets printed.

  2. last

    The "last" statement is used to exit the loop completely.
    foreach (1..10) { last if ($_ == 9); print("$_ "); }
    And here's the output:

    1 2 3 4 5 6 7 8

  3. redo

    And finally, the redo statement lets you restart a particular iteration of the loop:
    foreach (1..10) { print("$_ "); if ($_ == 9 && $flag != 1) { $flag=1; redo; } }
    In this case, here's what you'll see:

    1 2 3 4 5 6 7 8 9 9 10

In reply to Re: A few random questions from Learning Perl 3 by pg
in thread A few random questions from Learning Perl 3 by sulfericacid

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (6)
    As of 2020-02-17 09:03 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (71 votes). Check out past polls.