Logo         Community
The Company
Your Account
Contact Us
Perl 101 (part 3): Looping The Loop
Learn about Perl's control structures and array variables.

| While You Were Sleeping... |

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)

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.


# weighted employee evaluation program
# call me WEEP!

# ask the question
print ("Are you satisfied with your salary? [y/n] ");

# get an answer
$reply = <STDIN>;

# keep asking until you get the reply you want
while($reply ne 'y')
print ("Are you satisfied with your salary? [y/n] ");
$reply = <STDIN>;

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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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:


# factorials

# initialize a variable
$factorial = 1;

# ask for a number.
print ("Gimme a number!\n");

# process it
$number = <STDIN>;

# assign the number to a "temp" variable
$tmpnumber = $number;

# calculate the factorial
while($number != 1)
$factorial = $factorial * $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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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.

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback