REVISED: Sunday, March 3, 2013
You will learn how to use the Perl foreach loop.
I. PERL FOREACH LOOP
The basic kinds of loops in Perl are the for loop, the while loop, and the foreach loop. The foreach loop is a form of the for loop.
The foreach loop is used to iterate through the elements of an array.
The foreach loop can be used to:
A. Find the largest element in an array.
B. Print the elements of an array.
C. Determine if a certain value is one of the elements of an array.
II. PERL FOREACH LOOP SYNTAX
The syntax for the foreach loop is as follows:
foreach loopVariable (ARRAY) {
statements;
}
The loopVariable is assigned the value of each array element, until the end of the array is reached.
III. PERL FOREACH LOOP EXAMPLE
In addition to the Perl builtin system functions Perl gives you the ability to make subroutines, which are user-defined functions. The Perl builtin system functions can be combined and incorporated into your user-defined functions.
The following subroutine uses the foreach loop to iterate through the elements of an array:
#Subroutine max definition
#Parameter array @_
sub max {
my($max) = shift(@_);
foreach my $temp (@_) {
$max = $temp if $temp > $max;
}
return($max);
}
IV. PERL FOREACH LOOP SOURCE CODE
First we need to create the text input data file.
A Perl script is just a text file. Perl script files end with the .pl extension.
"Copy Paste" the following Perl program into your text editor:
#Unicode
use utf8;
#Provides undefined value warnings.
use warnings;
#enables -w flag.
#Turn on verbose warnings.
use diagnostics;
#Helps you catch typos.
#Forces you to use my( ) function to declare all variables.
#my variables (lexical variables) are faster than globals.
use strict;
#Stack trace.
use Carp ();
local $SIG{__WARN__} = \&Carp::confess;
open STDOUT, ">c:/strawberry/array.txt";
print "0\n";
print "1\n";
print "2\n";
print "3\n";
print "4\n";
print "5\n";
print "6\n";
print "7\n";
print "8\n";
print "9\n";
close STDOUT;
"File Save As" writeArray.pl using the path of the Perl download; e.g.:
c:\strawberry\writeArray.pl
Depending on your platform you will have a Perl prompt as follows:
C:\WINDOWS\system32>
From the above Perl prompt type:
C:\WINDOWS\system32>c:\strawberry\writeArray.pl
Press Enter.
Perl will display the following on your Perl prompt:
C:\WINDOWS\system32>
The Perl prompt screen is blank because the numbers we plan to load into our array were printed to the array.txt file on your hard drive and not your Perl prompt screen.
Now we have a text file named array.txt which contains the numbers we want to read from the text file into an array.
B. PROCESS DATA FILE
"Copy Paste" the following Perl program into your text editor:
use utf8;
#Provides undefined value warnings.
use warnings;
#enables -w flag
#Turn on verbose warnings.
use diagnostics;
#Helps you catch typos.
#Forces you to use my( ) function to declare all variables.
#my variables (lexical variables) are faster than globals.
use strict;
#Stack trace.
use Carp ();
local $SIG{__WARN__} = \&Carp::confess;
#Subroutine max definition
#Parameter array @_
sub max {
my($max) = shift(@_);
foreach my $temp (@_) {
$max = $temp if $temp > $max;
}
return($max);
}
#Specify the file.
my $file = "c:/strawberry/array.txt";
#Open the file and read data.
#Die with grace if it fails.
open (FILE, "<$file") or die "Can't open $file: $!\n";
#Read text data file into array @lines.
my @lines = <FILE>;
#Call (invoke) subroutine and print return.
print &max(@lines);
#Close file handle FILE.
close FILE;
"File Save As" readArray.pl using the path of the Perl download; e.g.:
c:\strawberry\readArray.pl
From the Perl prompt window type the following:
C:\WINDOWS\system32>c:\strawberry\readArray.pl
Press Enter and the following will display on your Perl prompt window:
9
C:\WINDOWS\system32>
The above example demonstrates the basic steps you will perform when you want to use data file input with your Perl programs.
V. PERL FOREACH LOOP SUMMARY
When you want to solve a problem that includes using an array always think about how the foreach loop can help you. Using the foreach loop we easily found the largest element in our array. All we needed was data to load into the array. The data will be the information you want to process.
You have learned how to use the Perl foreach 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