Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

loops

for ($i = 0; $i <= $repeats-1; $i++) {body;}

$i=0;

do

{print("$i\n");

   $i++;

} while($i < 10);

 

 

New Perl Loops And Syntax

Here's some of the new loops and modified C loops Perl offers.

    until
    It's the same as a while loop but it evaluates the control statement as if it were false instead of true. It's like the unless statement.

    $i=0;

    until($i >= 10)

    {

       print($i\n");

    }

    do {} until
    Same thing as a do {} while but uses the same until above as the control. The loop won't stop until

    $i=0;

    do

    {

       print("$i\n");

    } until($i > 10);

    foreach
    This is a nice feature of Perl. It takes an array as an argument and the runs through each element in order until gets to the end of the array. There are two ways you can use foreach. You can refer to the current element visited with $_ or you can specify a variable into which the current element is to be stored.

    Version 1

    @mylist=qw(Roger Duane Rerun Dee Momma Shirley);

    foreach(@mylist)

    {

       print("$_ was in What's Happening \n");

    }

    Version 2

    @mylist=qw(Roger Dwayne Rerun Dee Momma Shirley);

    foreach $member (@mylist)

    {

       print("$member was on What's Happening \n");

    }

    Each time through the loop, $_ and $member contain a new value. You can also the foreach with modify the way the array is evaluated when it's in the foreach loop. Here's some examples.

    • foreach (sort @mylist)- will go through the list in ascending ASCII order.
    • foreach (reverse @mylist)- will start from the last element and go to the first.
    • foreach (reverse sort @mylist)- will go through the array in descending ASCII order.

 

 

Loop Features And Flow Control

Now here's something that's nice. Perl has some commands that can kick you out of a loop, bring you to the next iteration of the loop while skipping the rest of the code, and re-do the current iteration of the loop.


    last
    Takes you out of the current loop you are in. Here's an example

    for($i=0; $i < 20; $i++)

    {

       last if ($i==7);

       printf("$i \n");

    }

    Yeah, it's a pointless example, but it works. As soon as i equals 7, the loop stops. This command can only be used on for, foreach, while, and until loops. It will not work with if|unless/else statements or do {} while/until loops. I can understand why it won't work in an if/else because it's not a loop. But do is and the only reason I can think of why do doesn't work is because the loop control is on the bottom whereas all the others it's on the top. Anyway, I've tried it and it doesn't' work. So that's the end of that.

    next
    This is similar to last except that it skips the rest of the code in the loop wherever it's called and goes back to the start. And like last, next cannot be used on if/else and do {} statements. Here's another pointless example.

    $i=0;

    while($i < 20)

    {

       $i++;

       next if ($i == 7);

       print("$i \n");

    }

    This will print every number from 1 to 20 except for 7.

    redo
    You guessed it, this will redo the current loop block without touching the control statement. And once again, it won't work on if/else statements or do loops. Here's an example:

    $i=0;

    while($i < 50)

    {

       $i++;

       redo if (($i**2)%2);

       print("$i \n");

    }

    This will print out all even squares of numbers 1 to 50.

    Labeling Loop Blocks
    You may be wondering what happens when you have a nested loop containing a next, last or redo. As may be expected, it would only work on which ever loop contains the next, last, or redo. If you have a program with 2 or 3 nested loops and you want to get out to the outermost loop, you need to label your blocks and specify the label in the last (next or redo). Here's an example.

    LOOP1: for($i=0; $i < 50; $i++)

    {

       LOOP2: for($j=0; $j < 10; $j++)

       {

          LOOP3: for($k=0; $k < 30; $k++)

          {

             last LOOP1 if ($k == (2*$i) == (3*$j));

          }

       }

    }

    Yup, this example is pointless, but it shows how labels work. Once k is 6, i is 3, and j is 2, the last will be called. This will exit out of LOOP1 rather than LOOP3.

    Single Line Loops
    Like the if statement, you can also produce single line while loops and until loops. Here's an example

    $i=2;

    $i++ while ($i <= 10);

    That should make i equal to 10 without having to put in a set of curly braces. The one thing you can't do is nest single line loops in a single line loop.