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

Playing With Friends - 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
Now that you've got the basics of array variables down, it's time for us tointroduce the "foreach" loop, one of the most useful control structures inPerl for dealing with array variables. It looks like this:

foreach some_scalar_variable (some_array_variable) { do this! }
Here's an example:

#!/usr/bin/perl @friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross"); foreach $item (@friends) { print("$item is a true Friend!\n"); }
And the output is:

Rachel is a true Friend! Monica is a true Friend! Phoebe is a true Friend! Chandler is a true Friend! Joey is a true Friend! Ross is a true Friend!
The "foreach" loop works its way through the elements of an array,assigning each element to the defined scalar variable, and then executingthe statements within the curly braces. In the example above, eachsubsequent iteration of the loop sees a new value being assigned to thescalar variable $item - this continues as many times as there are elementsin the array. Once all the array elements are exhausted, the statementsfollowing the loop are executed.

An important point to note here is that the scalar variable used in the"foreach" loop is only assigned a value temporarily - once the loop hasfinished executing, the original value of the scalar variable, if any, isrestored. Take a look:

#!/usr/bin/perl @friends = ("Rachel", "Monica", "Phoebe", "Chandler", "Joey", "Ross"); $item = "Superman"; foreach $item (@friends) { print("$item is a true Friend!\n"); } print ("Loop done!\n"); print ('The value of $item is now ', $item);
Here's what you'll get

Rachel is a true Friend! Monica is a true Friend! Phoebe is a true Friend! Chandler is a true Friend! Joey is a true Friend! Ross is a true Friend! Loop done! The value of $item is now Superman


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: