Friday, November 18, 2011

PERL FOR LOOP

PERL FOR LOOP


 

REVISED: Sunday, March 3, 2013




You will learn how to use the Perl for loop.

I.  PERL FOR LOOP

The basic kinds of loops in Perl are the for loop, the while loop, and the foreach loop. The Perl for loop is used to run a block of code a specific number of times.

The Perl for loop syntax is as follows:

for(initialization; test; increment){
   block of statements;
}

The for loop has three arguments separated by two semicolons.

Step 1. The first argument, the counter, is initialized.  Step 1. is executed only once at the beginning of the for statement.

Step 2. The second argument, the test, is evaluated. If the test evaluates to true, the block of statements are executed; otherwise the for loop is terminated.

Step 3. After the block of statements, enclosed by the curly braces, are executed, the counter is incremented by the third argument and "Step 2." is repeated.

Perl loops through "Step 2." and "Step 3." until the test expression is false and the for loop is terminated.

If the test expression is never false, you have an infinite loop which can be stopped by pressing Ctrl+c.

Here is a code snippet to print a message 10 times.

for($counter = 1; $counter <= 10; $counter++){
    print "for loop #$counter\n";
}

Step 1. $counter is initialized to 1.

Step 2. Perl checks to see if the $counter is less than or equal to 10.  $counter is 1; the test is true therefore, Perl executes the code inside the block.

Step 3. Perl increases $counter by 1.

The process continues looping through "Step 2." and "Step 3." until $counter is equal to 11 at which time the test is false.

$counter is no longer less than or equal to 10; therefore, the loop exits, after the code block has executed 10 times.

II.  PERL FOR LOOP SOURCE CODE

You can initialize the counter outside the loop, and increment the control variable inside the block as shown by the following example.

Copy paste the following into your text editor.

#Unicode
use utf8;
#Provides undefined value warnings.
use warnings;
#enables -w flag.

use diagnostics;
#Helps you catch typos.
#Forces you to use my() function to declare all variables.
use strict;

#stack trace
use Carp ();
      local $SIG{__WARN__} = \&Carp::confess;

#Specify the file.
my($file) = "C:/Strawberry/forLoop.pl";

#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";

my($counter) = 1;
for(; $counter <= 10;){
     print "for loop #$counter\n";
     $counter++;
}

#Close the file.
close FILE;

From your text editor, "File Save As" forLoop.pl using the path to the folder of your Perl download.

From the Perl prompt type the following:
C:\WINDOWS\system32>C:\Strawberry\forLoop.pl

Press Enter and the following will display on your screen:

C:\WINDOWS\system32>C:\Strawberry\forLoop.pl
for loop #1
for loop #2
for loop #3
for loop #4
for loop #5
for loop #6
for loop #7
for loop #8
for loop #9
for loop #10

C:\WINDOWS\system32>

You have learned how to use the Perl for loop.

Elcric Otto Circle



-->   -->   -->







How to Link to My Home Page

It will appear on your website as:
"Link to ELCRIC OTTO CIRCLE's Home Page"






No comments:

Post a Comment