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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PERL

Perl 101 (Part 3) - Looping The Loop
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 to introduce the "foreach" loop, one of the most useful control structures in Perl 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 executing the statements within the curly braces. In the example above, each subsequent iteration of the loop sees a new value being assigned to the scalar variable $item - this continues as many times as there are elements in the array. Once all the array elements are exhausted, the statements following 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 has finished executing, the original value of the scalar variable, if any, is restored. 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

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek