Perl Conditionals - The While Loop (
Page 4 of 4 )
The While Loop loops until a condition you give it is false.
#!/usr/bin/perl
$count= 1
while ($count<10)
{
print "$countn";
$count++;
}
The above code will, like the code before it, print out the numbers 1 through 9, stopping once the value becomes equal to ten and the statement is false.
Foreach There is Another!
Our final Loop is known as the Foreach Loop and is used mostly with arrays.
#!/usr/bin/perl
@breakfast = ("Sausage", "Eggs", "Bacon", "Grits", "Toast" "Hash");
print "My breakfast:nn";
foreach $breakfast (@breakfast)
{
print "$breakfastn";
}
This code access the values in your array and prints them one by one:
Sausage
Eggs
Bacon
Grits
Toast
Hash
And that is it for Loops and Conditionals. In my next tutorial, I will go over a few more statements, and we will learn to work with files -- creating them, reading them, writing them, you name it.
So go forth, young nerdling, and use your Perl wisdom for great deeds. Till then...