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

For Pete's Sake! - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Both "while" and "until" are typically used when you don't know for certainhow many times the program should loop - in the examples above, forexample, the program continues to loop until the user enters the rightanswer. But Perl also comes with a mechanism for executing a set ofstatements a specific number of times - and it's called the "for" loop:

for (initial value of counter; condition; update counter) { do this! }
Doesn't make any sense? Well, the "counter" here refers to a scalarvariable that is initialized to a specific numeric value [usually 0 or 1];this counter is used to keep track of the number of times the loop has beenexecuted.

Each time the loop is executed, the "condition" is tested for validity. Ifit's found to be valid, the loop continues to execute and the value of thecounter is updated appropriately; if not, the loop is terminated and thestatements following it are executed.

Take a look at this simple example of how the "for" loop can be used:

#!/usr/bin/perl for ($a=5;$a<12;$a++) { print("It's now $a PM. Too early!\n"); } print("Let's party!\n");
Here's what the output looks like:

It's now 5 PM. Too early! It's now 6 PM. Too early! It's now 7 PM. Too early! It's now 8 PM. Too early! It's now 9 PM. Too early! It's now 10 PM. Too early! It's now 11 PM. Too early! Let's party!
How does this work? We've begun by initializing the variable $a to 5. Eachtime the loop is executed, it checks whether or not $a is less than 12; ifit is, a line of output is printed and the value of $a is increased by 1 -that's where the $a++ comes in. Once the value of $a reaches 12, the loopis terminated and the line following it is executed.

And, for something slightly more complex, take a look at our re-write ofthe factorial calculator above:

#!/usr/bin/perl # factorials version 2.0 # ask for a number. print ("Gimme a number!\n"); # process it $number = <STDIN>; chomp($number); # use the FOR loop to calculate the factorial # note how we've initialized variables within the # loop itself - you can do this too! for ($factorial=1,$counter = $number; $counter > 1; $counter--) { $factorial = $factorial * $counter; } print ("The factorial of $number is $factorial.\n");


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 PROGRAMMING ARTICLES

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