ch03_06.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 72 行
HTM
72 行
<html><head><title>The foreach Control Structure (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch03_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.6. The foreach Control Structure</h2><p>It's handy to be able to process an entire array or list, soPerl provides a control structure to do just that. The <tt class="literal">foreach</tt><a name="INDEX-294" /> loop steps through a list of values,executing one iteration (time through the loop) for each value:</p><blockquote><pre class="code">foreach $rock (qw/ bedrock slate lava /) { print "One rock is $rock.\n"; # Prints names of three rocks}</pre></blockquote><p>The control variable (<tt class="literal">$rock</tt> inthat example) takes on a new value from the list for each iteration.The first time through the loop, it's <tt class="literal">"bedrock"</tt>; the third time, it's<tt class="literal">"lava"</tt>.</p><p>The control variable is not a copy of the list element -- itactually <em class="emphasis">is</em> the list element. That is, if youmodify the control variable inside the loop, you'll bemodifying the element itself, as shown in the following code snippet.This is useful, and supported, but it would surprise you if youweren't expecting it.</p><blockquote><pre class="code">@rocks = qw/ bedrock slate lava /;foreach $rock (@rocks) { $rock = "\t$rock"; # put a tab in front of each element of @rocks $rock .= "\n"; # put a newline on the end of each}print "The rocks are:\n", @rocks; # Each one is indented, on its own line</pre></blockquote><p>What is the value of the control variable after the loop hasfinished? It's the same as it was before the loop started. Thevalue of the control variable of a <tt class="literal">foreach</tt> loop is automatically saved andrestored by Perl. While the loop is running, there's no way toaccess or alter that saved value. So after the loop is done, thevariable has the value it had before the loop, or <tt class="literal">undef</tt> if it hadn't had a value. Thatmeans that if you want to name your loop control variable"<tt class="literal">$rock</tt>", youdon't have to worry that maybe you've already used thatname for another variable.<a href="#FOOTNOTE-82">[82]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-82" /><p>[82]Unless the variable namehas been declared as a lexical in the current scope, in which caseyou get a lexically local variable instead of a package localvariable -- more on this in <a href="ch04_01.htm">Chapter 4, "Subroutines"</a>.</p></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch03_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.5. Interpolating Arrays into Strings</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">3.7. Perl's Favorite Default: $_</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?