![]() |
|
Perl Tutorial #3 Now you are familiar with the concept of loops, there are a couple more to become aquainted with. #! /usr/bin/perl
my $i = 0;
while($i < 5)
{
print "i = $i \n";
$i++;
}
exit(0);
The while loop is slightly different because some of the work that the for loop does has to be done by the programmer but is much more flexible. First we set a variable $i (i means index) then we start the loop which only waits for the value of $i to reach 4 as before (not 5 remember because we start from zero). The value of $i is printed and then incremented by one and then it goes around again as before and will give the same output as before. So why use it instead of the for loop? Try to guess what would happen if the $i++ statement was before the echo statement. You would get this:- i = now: 1 Consider what is happening. The variable starts as zero but it is incremented before it's value is printed. This is just part of the flexibility of the while statement and we will return to it in a later tutorial. Now another loop:- #! /usr/bin/perl
my $i = 0;
until($i==5)
{
print "i = $i\n";
$i++;
}
exit(0);
Alright, that's enough loops. On to something a little more interesting. #! /usr/bin/perl
my $i = 0;
while($i < 5)
{
if($i == 3)
{
last;
}
print "i = $i \n";
$i++;
}
exit(0);
The if statement will choose to do something based on certain criteria. As before we have a while loop which will continue as before until the value of $i equals 3 in which case it will break out of the loop (using the keyword last which works in the same way that the the C break command does) and exits in the normal way. This produces the ouput:- i = 0 If can be a very useful command. Once again try different values for the if statement to see what happens. However, it can be a pain to use if you have a large number of criteria. In C this is helped by the Case...Switch commands. On first inspection, it seems that Perl doesn't support it. However, later versions do support it provided the relevent library is installed (Mandarke 10.0 does support it) and here is how it looks:- #! /usr/bin/perl
use Switch;
$val=3;
switch ($val)
{
case 1 { print "number 1\n" }
case 2 { print "number 2\n" }
case 3 { print "number 3\n" }
case 4 { print "number 4\n" }
case 5 { print "number 5\n" }
else { print "Some other number\n" }
}
exit(0);
The switch statement will switch between the various cases until a match is found. The $val is preset with the value you want to check for and the relevent case statement prints a helpful string such as: number 3 If $val doesn't contain a relevent number (i.e. 6) then it will print:- Some other number It is important that use Switch is included as written and ending with a semi-colon as this is how Perl includes library functions (equivalent to the C include command). Also C programmers note that else is used instead of default if none of the case statements is activated. If you wish to search for strings then the case being searched for should be surrounded with double quotes as in: #! /usr/bin/perl
use Switch;
$val="3";
switch ($val)
{
case "1" { print "number 1\n" }
case "2" { print "number 2\n" }
case "3" { print "number 3\n" }
case "4" { print "number 4\n" }
case "5" { print "number 5\n" }
else { print "Some other number\n" }
}
exit(0);
That concludes this tutorial... Please eMail me at: nickjc@nickjc.co.uk if you need help |
|