⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch03_05.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Interpolating Arrays into Strings (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 &amp; 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_04.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_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.5. Interpolating Arrays into Strings</h2><p>Like scalars, array values may be interpolated into a<a name="INDEX-291" />double-quoted string. Elements of anarray are automatically separated by spaces<a href="#FOOTNOTE-79">[79]</a> upon interpolation:</p><blockquote class="footnote"> <a name="FOOTNOTE-79" /><p>[79]Actually,the separator is the value of the special <a name="INDEX-292" /><tt class="literal">$"</tt>variable, which is a space bydefault.</p> </blockquote><blockquote><pre class="code">@rocks = qw{ flintstone slate rubble };print "quartz @rocks limestone\n";  # prints five rocks separated by spaces</pre></blockquote><p>There are no extra spaces added before or after an interpolatedarray; if you want those, you'll have to put them in yourself:</p><blockquote><pre class="code">print "Three rocks are: @rocks.\n";print "There's nothing in the parens (@empty) here.\n";</pre></blockquote><p>If you forget that arrays interpolate like this, you'll besurprised when you put an <a name="INDEX-293" />email address into adouble-quoted string. For historical reasons,<a href="#FOOTNOTE-80">[80]</a>this is a fatal error at compile time:</p><blockquote class="footnote"> <a name="FOOTNOTE-80" /><p>[80]Sinceyou asked: Before version 5, Perl would silently leave uninterpolatedan unused array's name in a double-quoted string. So,<tt class="literal">"fred@bedrock.edu"</tt> might be a string containing anemail address. This attempt to Do What I Mean will backfire whensomeone adds a variable named <tt class="literal">@bedrock</tt>to the program -- now the stringbecomes <tt class="literal">"fred.edu"</tt> or worse.</p> </blockquote><blockquote><pre class="code">$email = "fred@bedrock.edu";  # WRONG! Tries to interpolate @bedrock$email = "fred\@bedrock.edu"; # Correct$email = 'fred@bedrock.edu';  # Another way to do that</pre></blockquote><p>A single element of an array will be replaced by its value, just asyou'd expect:</p><blockquote><pre class="code">@fred = qw(hello dolly);$y = 2;$x = "This is $fred[1]'s place";    # "This is dolly's place"$x = "This is $fred[$y-1]'s place"; # same thing</pre></blockquote><p>Note that the index expression is evaluated as an ordinaryexpression, as if it were outside a string. It is <em class="emphasis">not</em> variable-interpolated first. In otherwords, if <tt class="literal">$y</tt> contains the string<tt class="literal">"2*4"</tt>, we're still talkingabout element 1, not element 7, because <tt class="literal">"2*4"</tt> as a number (the value of <tt class="literal">$y</tt> used in a numeric expression) is justplain 2.<a href="#FOOTNOTE-81">[81]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-81" /><p>[81]Of course, if you've got warningsturned on, Perl is likely to remind you that<tt class="literal">"2*4"</tt>is apretty funny-looking number.</p> </blockquote><p>If you want to follow a simple scalar variable with a left squarebracket, you need to delimit the square bracket so that itisn't considered part of an array reference, as follows:</p><blockquote><pre class="code">@fred = qw(eating rocks is wrong);$fred = "right";               # we are trying to say "this is right[3]"print "this is $fred[3]\n";    # prints "wrong" using $fred[3]print "this is ${fred}[3]\n";  # prints "right" (protected by braces)print "this is $fred"."[3]\n"; # right again (different string)print "this is $fred\[3]\n";   # right again (backslash hides it)</pre></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_04.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_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.4. List Assignment</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.6. The foreach Control Structure</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 &copy; 2002</a> O'Reilly &amp; 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -