📄 516-518.html
字号:
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Perl</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=28//-->
<!--PAGES=516-518//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="513-516.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="519-521.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading14"></A><FONT COLOR="#000077">for Statements</FONT></H4>
<P>So far, the only Perl programming constructs we’ve looked at have been conditional statements. The <TT>for</TT> 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 <TT>for</TT> statement is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
for (initialize_expression; test_expression; increment_expression) {
statements;
}
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>initialize_expression</TT> is used to initialize the <TT>increment</TT> variable that is being used by the statement. This expression is evaluated only the first time the <TT>for</TT> statement executes. The <TT>test_expression</TT> evaluates a conditional expression that is used to determine whether to execute the statement block. If the <TT>test_expression</TT> evaluates to True, the statement block is executed; if it evaluates to False, the statement block is not executed. The <TT>test_expression</TT> is executed before each iteration of the <TT>for</TT> statement. The last expression in the <TT>for</TT> statement is the <TT>increment_expression</TT>. This expression is typically used to change the <TT>increment</TT> variable. This expression is executed after each execution of the statement block.</P>
<P>The following example illustrates the processing performed by a <TT>for</TT> statement:</P>
<!-- CODE SNIP //-->
<PRE>
for ($i = 0; $i < 20; $i = $i + 2) {
print $i * 2, “ “;
}
print “\nBedeba bedeba, That’s all folks.\n”
</PRE>
<!-- END CODE SNIP //-->
<P>This program prints the value of <TT>$i * 2</TT> each time through the loop. Before the loop is executed the first time, <TT>$i</TT> is assigned a value of 0. Next the value of <TT>$i</TT> is compared to 20 and because it is less than 20, the statement block is executed. The statement block prints <TT>0</TT> followed by a space on the screen. Now the increment expression is evaluated. This results in <TT>$i</TT> being assigned the value of <TT>2</TT>. The <TT>for</TT> statement continues executing in this manner until the <TT>test_expression</TT> evaluates to False (when <TT>$i</TT> is no longer less than 20). When this happens, the program continues executing at the statement that directly follows the <TT>for</TT> statement—in this case, the <TT>That’s all folks</TT> print statement.</P>
<P>The output generated by running this program is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
0 4 8 12 16 20 24 28 32 36
Bedeba bedeba, That’s all folks.
</PRE>
<!-- END CODE SNIP //-->
<P><TT>for</TT> statements are often used in conjunction with fixed-size arrays. This is because the iterative nature of a <TT>for</TT> 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:</P>
<!-- CODE SNIP //-->
<PRE>
for ($i=0; $i < 50; $i = $i + 1) {
print $array[$i], “\n”
}
</PRE>
<!-- END CODE SNIP //-->
<P>When you do not know the size of an array (which is often the case when you are writing programs in Perl), the <TT>foreach</TT> or <TT>while</TT> statements are more useful for performing array manipulation.</P>
<H4 ALIGN="LEFT"><A NAME="Heading15"></A><FONT COLOR="#000077">foreach Statements</FONT></H4>
<P>Another iteration statement available in Perl is the <TT>foreach</TT> statement. This statement is very similar to the <TT>for</TT> statement in the <TT>bash</TT> and <TT>pdksh</TT> languages and the <TT>foreach</TT> statement in the <TT>tcsh</TT> language. It executes its statement block for each element in the list that is specified as its second argument. The syntax for the <TT>foreach</TT> statement is</P>
<!-- CODE SNIP //-->
<PRE>
foreach $i (@list) {
statements;
}
</PRE>
<!-- END CODE SNIP //-->
<P>The <TT>@list</TT> variable can be passed to the <TT>foreach</TT> statement either as a variable or as a list of elements that you type directly as the second argument to the <TT>foreach</TT> statement. The following is an example of the <TT>foreach</TT> statement:</P>
<!-- CODE SNIP //-->
<PRE>
@myarray = (5,10,15,20,25);
foreach $i (@myarray) {
print $i * 3, “ “;
}
print “\n”;
</PRE>
<!-- END CODE SNIP //-->
<P>This program iterates through each element in <TT>@myarray</TT> and prints the result of three times that array element on the screen. The output generated by running this program is as follows:</P>
<!-- CODE SNIP //-->
<PRE>
15 30 45 60 75
</PRE>
<!-- END CODE SNIP //-->
<P>Another interesting use of the <TT>foreach</TT> 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 <TT>foreach</TT> 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.</P>
<!-- CODE SNIP //-->
<PRE>
@myarray = (5,10,15,20,25);
foreach $i (@myarray) {
$i = $i * 3;
}
</PRE>
<!-- END CODE SNIP //-->
<P>After this code executes, <TT>@myarray</TT> is equal to <TT>(15,30,45,60,75)</TT>.</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">while Statements</FONT></H4>
<P>The <TT>while</TT> statement is another form of iteration statement that Perl provides. The <TT>while</TT> statement executes its statement block while a conditional expression is True. The syntax for the <TT>while</TT> statement is</P>
<!-- CODE SNIP //-->
<PRE>
while (expression) {
statements;
}
</PRE>
<!-- END CODE SNIP //-->
<P>When the <TT>while</TT> 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 <TT>while</TT> statement’s statement block and continues there. Here is an example of the <TT>while</TT> statement:</P>
<!-- CODE SNIP //-->
<PRE>
$i = 0;
@temparray = (1,2,3,4,5,6,7);
while ($temparray[$i]) {
print $temparray[$i];
$i = $i + 1;
}
</PRE>
<!-- END CODE SNIP //-->
<P>This example simply prints each of the array elements contained in <TT>@temparray</TT> to the screen. Notice that you do not need to know how many elements are in the array. The <TT>while</TT> statement continues executing until the value of <TT>$temparray[$i]</TT> is undefined, which occurs when it tries to access an element that is outside the bounds of the array.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="513-516.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="519-521.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -