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.
The not-so-distant cousin of Perl's "while" loop is its "until" loop, whichlooks like this:
until (condition)
{
do this!
}
To illustrate the relationship between the "while" and "until" loops, readthese two sentences:
WHILE you're less than twenty-one years of age, you can't drink!
UNTIL you're over twenty-one years of age, you can't drink!
In other words, the conditional expression to be evaluated in a "while"loop will be exactly the opposite of the one to be evaluated in an "until"loop. So take another look at the WEEP, which we've rewritten using an"until" statement:
#!/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
until($reply eq '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");
If you compare the "while" and "until" lines in the WEEP examples above,you'll see that the two conditional expressions are exactly the opposite ofeach other. And if you can't decide which one to use, try this little trick- translate your "until" or "while" statement into English, roll it aroundyour tongue, and see if it sounds right to you...
This article copyright Melonfire 2000. All rights reserved.