Perl
  Home arrow Perl arrow Page 8 - 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 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 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


    Perl 101 (Part 3) - Looping The Loop - Playing With Friends


    (Page 8 of 9 )

    Now that you've got the basics of array variables down, it's time for us tointroduce the "foreach" loop, one of the most useful control structures inPerl for dealing with array variables. It looks like this:

    foreach some_scalar_variable (some_array_variable) { do this! }
    Here's an example:

    #!/usr/bin/perl @friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross"); foreach $item (@friends) { print("$item is a true Friend!\n"); }
    And the output is:

    Rachel is a true Friend! Monica is a true Friend! Phoebe is a true Friend! Chandler is a true Friend! Joey is a true Friend! Ross is a true Friend!
    The "foreach" loop works its way through the elements of an array,assigning each element to the defined scalar variable, and then executingthe statements within the curly braces. In the example above, eachsubsequent iteration of the loop sees a new value being assigned to thescalar variable $item - this continues as many times as there are elementsin the array. Once all the array elements are exhausted, the statementsfollowing the loop are executed.

    An important point to note here is that the scalar variable used in the"foreach" loop is only assigned a value temporarily - once the loop hasfinished executing, the original value of the scalar variable, if any, isrestored. Take a look:

    #!/usr/bin/perl @friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross"); $item = "Superman"; foreach $item (@friends) { print("$item is a true Friend!\n"); } print ("Loop done!\n"); print ('The value of $item is now ', $item);
    Here's what you'll get

    Rachel is a true Friend! Monica is a true Friend! Phoebe is a true Friend! Chandler is a true Friend! Joey is a true Friend! Ross is a true Friend! Loop done! The value of $item is now Superman


    This article copyright Melonfire 2000. All rights reserved.

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


     

       

    PERL ARTICLES

    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl
    - 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





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