516 518




Linux Unleashed, Third Edition:Perl





-->















Previous
Table of Contents
Next




for Statements
So far, the only Perl programming constructs we’ve looked at have been conditional statements. The for statement is an example of a Perl iteration statement. It is used to iterate over a statement block a specified number of times. The syntax of the for statement is as follows:


for (initialize_expression; test_expression; increment_expression) {
statements;
}


The initialize_expression is used to initialize the increment variable that is being used by the statement. This expression is evaluated only the first time the for statement executes. The test_expression evaluates a conditional expression that is used to determine whether to execute the statement block. If the test_expression evaluates to True, the statement block is executed; if it evaluates to False, the statement block is not executed. The test_expression is executed before each iteration of the for statement. The last expression in the for statement is the increment_expression. This expression is typically used to change the increment variable. This expression is executed after each execution of the statement block.
The following example illustrates the processing performed by a for statement:


for ($i = 0; $i < 20; $i = $i + 2) {
print $i * 2, “ “;
}
print “\nBedeba bedeba, That’s all folks.\n”


This program prints the value of $i * 2 each time through the loop. Before the loop is executed the first time, $i is assigned a value of 0. Next the value of $i is compared to 20 and because it is less than 20, the statement block is executed. The statement block prints 0 followed by a space on the screen. Now the increment expression is evaluated. This results in $i being assigned the value of 2. The for statement continues executing in this manner until the test_expression evaluates to False (when $i is no longer less than 20). When this happens, the program continues executing at the statement that directly follows the for statement—in this case, the That’s all folks print statement.
The output generated by running this program is as follows:


0 4 8 12 16 20 24 28 32 36
Bedeba bedeba, That’s all folks.


for statements are often used in conjunction with fixed-size arrays. This is because the iterative nature of a for statement is naturally suited for printing and performing other calculations on this kind of array. For example, the following code fragment can be used to print all the values in an array of size 50 to the screen:


for ($i=0; $i < 50; $i = $i + 1) {
print $array[$i], “\n”
}


When you do not know the size of an array (which is often the case when you are writing programs in Perl), the foreach or while statements are more useful for performing array manipulation.
foreach Statements
Another iteration statement available in Perl is the foreach statement. This statement is very similar to the for statement in the bash and pdksh languages and the foreach statement in the tcsh language. It executes its statement block for each element in the list that is specified as its second argument. The syntax for the foreach statement is


foreach $i (@list) {
statements;
}


The @list variable can be passed to the foreach statement either as a variable or as a list of elements that you type directly as the second argument to the foreach statement. The following is an example of the foreach statement:


@myarray = (5,10,15,20,25);
foreach $i (@myarray) {
print $i * 3, “ “;
}
print “\n”;


This program iterates through each element in @myarray and prints the result of three times that array element on the screen. The output generated by running this program is as follows:


15 30 45 60 75


Another interesting use of the foreach statement is to use it to change all the values in an array. This can be done by assigning a value to the variable that is being used by the foreach statement. For example, the following code multiplies each array element in the array by three and actually stores the result of this back into the array.


@myarray = (5,10,15,20,25);
foreach $i (@myarray) {
$i = $i * 3;
}


After this code executes, @myarray is equal to (15,30,45,60,75).
while Statements
The while statement is another form of iteration statement that Perl provides. The while statement executes its statement block while a conditional expression is True. The syntax for the while statement is


while (expression) {
statements;
}


When the while statement executes, it evaluates its expression. If the result is True, the statements contained in the statement block are executed. If the result of the expression is False, Perl goes to the first line after the while statement’s statement block and continues there. Here is an example of the while statement:


$i = 0;
@temparray = (1,2,3,4,5,6,7);
while ($temparray[$i]) {
print $temparray[$i];
$i = $i + 1;
}


This example simply prints each of the array elements contained in @temparray to the screen. Notice that you do not need to know how many elements are in the array. The while statement continues executing until the value of $temparray[$i] is undefined, which occurs when it tries to access an element that is outside the bounds of the array.



Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
Znaczenie mitu w życiu greków, podaj definicję pojęcia i~516
516 519
515 516
515 518
ReadMe (518)
516 GENERALMATIC
516 GENERALMATIC
515 518
515 518
pcs 516 uni
516 (2)
518,8,artykul
516,24,artykul

więcej podobnych podstron