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

...Or Until You Wake Up - 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 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.

>>> 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: