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 looks
like 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, which
evaluates to either true or false. So, were we to write the above example
in 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 who
work 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 should
be familiar to you by now - we're simply asking for input, assigning it to
a variable, and then using the chomp() function to remove the trailing
carriage return.
At this point, we begin checking the value of the variable - *while* this
value is *not equal* to "y", or an affirmative reply, we continue printing
the question over and over again, and stop only when the value becomes
equal to "y". At this point, the lines following the loop are executed, and
the appropriate output displayed.
The end result? A company full of "satisfied" employees [whoever says Perl
isn'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 conditional
expression 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 product
of 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 the
product of that number and the scalar variable $factorial [initialized to
1] - this value is stored in the variable $factorial. Next, the number is
reduced by 1, and the process is repeated, until the number becomes equal
to 1. At this stage, the value of $factorial is printed.
This article copyright Melonfire 2000. All rights reserved.