Perl
  Home arrow Perl arrow Page 2 - 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 
 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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    Perl 101 (Part 3) - Looping The Loop - While You Were Sleeping...


    (Page 2 of 9 )

    The most basic loop available in Perl is the "while" loop, and it lookslike this:

    while (condition) { do this! }
    Or, to make the concept clearer,

    while (rich Uncle Ed's still breathing) { be nice to him }
    The "condition" here is a standard Perl conditional expression, whichevaluates to either true or false. So, were we to write the above examplein Perl, it would look like this:

    while (rich_old_guy_lives == 1) { be_nice(); }
    How about an example you could actually use in your real-life job? Well,here's one - be warned, however, that it's primarily meant for managers whowork in the Dilbert Zone.

    #!/usr/bin/perl # weighted employee evaluation program # call me WEEP! # ask the question print ("Are you satisfied with your salary? [y/n] "); # get an answer $reply = <STDIN>; chomp($reply);
    # keep asking until you get the reply you want while($reply ne 'y') { print ("Are you satisfied with your salary? [y/n] "); $reply = <STDIN>; chomp($reply); } print ("Employee satisfaction has always been this company's goal.\n"); print ("Thank you for making this experience an enriching one!\n");

    And here's what it looks like:

    Are you satisfied with your salary? [y/n] n Are you satisfied with your salary? [y/n] n Are you satisfied with your salary? [y/n] n Are you satisfied with your salary? [y/n] n Are you satisfied with your salary? [y/n] y Employee satisfaction has always been this company's goal. Thank you for making this experience an enriching one!
    Let's take a closer look at this code. The first part of the program shouldbe familiar to you by now - we're simply asking for input, assigning it toa variable, and then using the chomp() function to remove the trailingcarriage return.

    At this point, we begin checking the value of the variable - *while* thisvalue is *not equal* to "y", or an affirmative reply, we continue printingthe question over and over again, and stop only when the value becomesequal to "y". At this point, the lines following the loop are executed, andthe appropriate output displayed.

    The end result? A company full of "satisfied" employees [whoever says Perlisn't powerful should take a look at that HR manager's Christmas bonus...]

    Here's another example, this one using a "while" loop and a conditionalexpression that contains a number instead of a string:

    #!/usr/bin/perl # factorials # initialize a variable $factorial = 1; # ask for a number. print ("Gimme a number!\n"); # process it $number = <STDIN>; chomp($number); # assign the number to a "temp" variable $tmpnumber = $number; # calculate the factorial while($number != 1) { $factorial = $factorial * $number; $number--; } print ("The factorial of $tmpnumber is $factorial.\n");
    In case you flunked math class, the factorial of a number X is the productof all the numbers between 1 and X. And here's what the output looks like:

    Gimme a number! 7 The factorial of 7 is 5040.
    And if you have a calculator handy, you'll see that

    7*6*5*4*3*2*1 = 5040

    Once the user enters a number, a "while" loop is used to calculate theproduct of that number and the scalar variable $factorial [initialized to1] - this value is stored in the variable $factorial. Next, the number isreduced by 1, and the process is repeated, until the number becomes equalto 1. At this stage, the value of $factorial is printed.

    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 4 hosted by Hostway