Part 3 in our continuing series on the popular scripting language, Perl. This week's article teaches you more about Perl's controlstructures - including the FOR and WHILE loops - and also introduces you toPerl's array variables.
The "for" loop also comes with a bunch of control statements, which can beused to modify its behaviour. We've listed the important ones below,together with examples:
next:
The "next" statement allows you to jump to the next iteration of the loopwithout executing the remaining statements of the current iteration.Consider this:
#!/usr/bin/perl
for ($counter=1; $counter<=10; $counter++)
{
if($counter == 9)
{
next;
}
print("$counter ");
}
print ("\nHey! Where did 9 go?\n");
print ("Simple. 7 8(ate) 9\n");
print ("Hey, if we were professional comedians, we wouldn't be here,
right?!\n");
Here's what it looks like:
1 2 3 4 5 6 7 8 10
Hey! Where did 9 go?
Simple. 7 8(ate) 9
Hey, if we were professional comedians, we wouldn't be here, right?!
As you can see, 9 is missing - this is because when the value of $counterhits 9, Perl uses the "next" statement to skip to the next iteration of theloop, and so 9 never gets printed.
last:
The "last" statement is used to exit the loop completely. Take a look: