Contents Perl loops

PERL - For Loops

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

1 This is row 1
2 This is row 2
3 This is row 3
4 This is row 4
5 This is row 5

We looped through one variable and incremented it. Using HTML, we were able to make a nice table to demonstrate our results.

PERL - Foreach Loops

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

1 Steve
2 Bill
3 Connor
4 Bradley

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

While 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 Redo

Outlined 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.

Next Place it inside your loop and it will stop the current iteration and go on to the next one.
Continue Executed after each loop iteration and before the conditional statement is evaluated. A good place to increment counters.
Last Last stops the looping immediately (like break)
Redo Redo will execute the same iteration over again.

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 Loop

Here 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 ???

 

 

Home