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...
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |