Perl
  Home arrow Perl arrow Page 6 - Perl 101 (Part 3) - Looping The Loop
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (Part 3) - Looping The Loop
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2000-06-15

    Table of Contents:
  • Perl 101 (Part 3) - Looping The Loop
  • While You Were Sleeping...
  • ...Or Until You Wake Up
  • Dos And Don'ts
  • For Pete's Sake!
  • Every Comedian Needs An Exit
  • Grade School
  • Playing With Friends
  • So Many Choices...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl 101 (Part 3) - Looping The Loop - Every Comedian Needs An Exit


    (Page 6 of 9 )

    The "for" loop also comes with a bunch of control statements, which can beused to modify its behaviour. We've listed the important ones below,together with examples:

    next:

    The "next" statement allows you to jump to the next iteration of the loopwithout executing the remaining statements of the current iteration.Consider this:

    #!/usr/bin/perl for ($counter=1; $counter<=10; $counter++) { if($counter == 9) { next; } print("$counter "); } print ("\nHey! Where did 9 go?\n"); print ("Simple. 7 8(ate) 9\n"); print ("Hey, if we were professional comedians, we wouldn't be here, right?!\n");
    Here's what it looks like:

    1 2 3 4 5 6 7 8 10 Hey! Where did 9 go? Simple. 7 8(ate) 9 Hey, if we were professional comedians, we wouldn't be here, right?!
    As you can see, 9 is missing - this is because when the value of $counterhits 9, Perl uses the "next" statement to skip to the next iteration of theloop, and so 9 never gets printed.

    last:

    The "last" statement is used to exit the loop completely. Take a look:

    #!/usr/bin/perl for($counter=1; $counter<=10; $counter++) { if($counter == 9) { last; } print("$counter "); }
    And here's the output:

    1 2 3 4 5 6 7 8
    redo:

    And finally, the redo statement lets you restart a particular iteration ofthe loop:

    #!/usr/bin/perl for($counter=1; $counter<=10; $counter++) { print("$counter "); if($counter == 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


    This article copyright Melonfire 2000. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway