Home arrow Perl Programming arrow Page 2 - Perl 101 (Part 3) - Looping The Loop

While You Were Sleeping... - Perl

Part 3 in our continuing series on the popular scripting language, Perl. This week's article teaches you more about Perl's controlstructures - including the FOR and WHILE loops - and also introduces you toPerl's array variables.

  1. Perl 101 (Part 3) - Looping The Loop
  2. While You Were Sleeping...
  3. ...Or Until You Wake Up
  4. Dos And Don'ts
  5. For Pete's Sake!
  6. Every Comedian Needs An Exit
  7. Grade School
  8. Playing With Friends
  9. So Many Choices...
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
June 15, 2000

print this article


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 Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: