| Contents | Perl loops | |||||||||||||||||||||
|
|
PERL - For LoopsA for loop counts through a range of numbers, running a block of code each time it iterates through the loop. The syntax is for($start_num, Range, $increment) { code to execute }. A for loop needs 3 items placed inside of the conditional statement to be successful. First a starting point, then a range operator, and finally the incrementing value. Below is the example. forloop.pl:#!/usr/bin/perl
print "content-type: text/html \n\n";
# SET UP THE HTML TABLE
print "<table border='1'>";
# START THE LOOP, $i is the most common counter name for a loop!
for($i = 1; $i < 5; $i++) {
# PRINT A NEW ROW EACH TIME THROUGH W/ INCREMENT
print "<tr><td>$i</td><td>This is row $i</td></tr>";
}
# FINISH THE TABLE
print "</table>";
forloop.pl:
We looped through one variable and incremented it. Using HTML, we were able to make a nice table to demonstrate our results. PERL - Foreach LoopsForeach is designed to work with arrays. Say you want to execute some code foreach element within an array. Here's how you might go about it. foreachloop.pl:#!/usr/bin/perl
print "content-type: text/html \n\n"; #The header
# SET UP THE HTML TABLE
print "<table border='1'>";
# CREATE AN ARRAY
@names = qw(Steve Bill Connor Bradley);
# SET A COUNT VARIABLE
$count = 1;
# BEGIN THE LOOP
foreach $names(@names) {
print "<tr><td>$count</td><td>$names</td></tr>";
$count++;
}
print "</table>";
foreachloop.pl:
We placed a table row counter for you to see each line more clearly. We use the variable $names to pull single elements from our array, PERL does the rest for us by looping through each element in our array. Use the sorting functions outlined in the PERL Arrays lesson.
PERL - WhileWhile loops continually iterate as long as the conditional statement remains true. It is very easy to write a conditional statement that will run forever especially at the beginner level of coding. On a more positive note, while loops are probably the easiest to understand. The syntax is while (coditional statement) { execute code; }. whilecounter.pl:#!/usr/bin/perl
print "content-type: text/html \n\n";
# SET A VARIABLE
$count = 0;
# RUN A WHILE LOOP
while ($count <= 7) {
# PRINT THE VARIABLE AND AN HTML LINE BREAK
print "$count<br />";
# INCREMENT THE VARIABLE EACH TIME
$count ++;
}
print "Finished Counting!";
whilecounter.pl:
0
1 2 3 4 5 6 7 Finished Counting! PERL - Next, Last, and RedoOutlined below are several interrupts that can be used to redo or even skip iterations of code. These functions allow you to control the flow of your while loops.
flowcontrol.pl:#!/usr/bin/perl
print "content-type: text/html \n\n";
# SET A VARIABLE
$count = 0;
while ($count <= 7) {
# SET A CONDITIONAL STATEMENT TO INTERRUPT @ 4
if ($count == 4) {
print "Skip Four!<br />";
next;
}
# PRINT THE COUNTER
print $count."<br />";
}
continue {
$count++;
};
print "Loop Finished!";
flowcontrol.pl:
0
1 2 3 Skip Four! 5 6 7 Finished Counting! Above, we skip the fourth iteration by incrementing the variable again. In the example we also print a line, "Skip Four!" just to make things easier to follow. PERL - While Array LoopHere we are just showing a method of looping through an array using a while loop. We use three variables to do this including: the array, a counter, and an index number so that each time the while loop iterates we also loop through each index of the array. whilearrayloop.pl:#!/usr/bin/perl
print "content-type: text/html \n\n";
# SET UP AN HTML TABLE
print "<table border='1'>";
# DEFINE AN ARRAY
@names = qw(Steve Bill Connor Bradley);
# COUNTER - COUNTS EACH ROW
$count = 1;
# COUNTS EACH ELEMENT OF THE ARRAY
$n = 0;
# USE THE SCALAR FORM OF ARRAY
while ($names[$n]) {
print "<tr><td>$count</td><td>$names[$n]</td></tr>";
$n++;
$count++;
}
print "</table>";
while.pl:1 Steve 2 Bill 3 Conner 4 Bradley Want Some more information and Video ???
|