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

📄 ch03_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>List Literals (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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.3. List Literals</h2><p>A <em class="emphasis">listliteral</em><a name="INDEX-268" /> (the way you represent a list valuewithin your program) is a list of comma-separated values enclosed inparentheses. These values form the elements of the list. For example:</p><blockquote><pre class="code">(1, 2, 3)      # list of three values 1, 2, and 3(1, 2, 3,)     # the same three values (the trailing comma is ignored)("fred", 4.5)  # two values, "fred" and 4.5( )             # empty list - zero elements(1..100)       # list of 100 integers</pre></blockquote><p>That last one uses the <em class="emphasis">.. range operator,</em><a name="INDEX-269" /> <a name="INDEX-270" />which is seen here for the first time. Thatoperator creates a list of values by counting from the left scalar upto the right scalar by ones. For example:</p><blockquote><pre class="code">(1..5)            # same as (1, 2, 3, 4, 5)(1.7..5.7)        # same thing - both values are truncated(5..1)            # empty list - .. only counts "uphill"(0, 2..6, 10, 12) # same as (0, 2, 3, 4, 5, 6, 10, 12)($a..$b)          # range determined by current values of $a and $b(0..$#rocks)      # the indices of the rocks array from the previous section</pre></blockquote><p>As you can see from those last two items, the elements of an arrayare not necessarily constants -- they can be expressions that willbe newly evaluated each time the literal is used. For example:</p><blockquote><pre class="code">($a, 17)       # two values: the current value of $a, and 17($b+$c, $d+$e) # two values</pre></blockquote><p>Of course, a list may have any scalar values, like this typical listof strings:</p><blockquote><pre class="code">("fred", "barney", "betty", "wilma", "dino")</pre></blockquote><a name="lperl3-CHP-3-SECT-3.1" /><div class="sect2"><h3 class="sect2">3.3.1. The qw Shortcut</h3><p>It turns out that lists of simple words (like the previous example)are frequently needed in Perl programs. The <tt class="literal">qw</tt><a name="INDEX-271" /><a name="INDEX-272" /> shortcut makes it easy to generate themwithout typing a lot of extra quote marks:</p><blockquote><pre class="code">qw/ fred barney betty wilma dino / # same as above, but less typing</pre></blockquote><p><tt class="literal">qw</tt> stands for "quotedwords" or "quoted by whitespace," depending uponwhom you ask. Either way, Perl treats it like a single-quoted string(so, you can't use <tt class="literal">\n</tt> or<tt class="literal">$fred</tt> inside a <tt class="literal">qw</tt> list as you would in a double-quotedstring). The whitespace (characters like spaces, tabs, and newlines)will be discarded, and whatever is left becomes the list of items.Since <a name="INDEX-273" />whitespaceis discarded, here's another (but unusual) way to write thatsame list:</p><blockquote><pre class="code">qw/fred  barney     bettywilma dino/   # same as above, but pretty strange whitespace</pre></blockquote><p>Since <tt class="literal">qw</tt> is a form of<a name="INDEX-274" />quoting, though, you can't putcomments inside a <tt class="literal">qw</tt> list.</p><p>The previous two examples have used forward slashes as the<a name="INDEX-275" />delimiter, but Perl actually lets youchoose any punctuation character as the delimiter. Here are some ofthe common ones:</p><blockquote><pre class="code">qw! fred barney betty wilma dino !qw# fred barney betty wilma dino #   # like in a comment!qw( fred barney betty wilma dino )qw{ fred barney betty wilma dino }qw[ fred barney betty wilma dino ]qw&lt; fred barney betty wilma dino &gt;</pre></blockquote><p>As those last four show, sometimes the two delimiters can bedifferent. If the opening delimiter is one of those"left" characters, the corresponding "right"character is the proper closing delimiter. Other delimiters use thesame character for start and finish.</p><p>If you need to include the closing delimiter<em class="emphasis">within</em> the string as one of the characters, youprobably picked the wrong delimeter. But even if you can't ordon't want to change the delimiter, you can still include thecharacter using the backslash:</p><blockquote><pre class="code">qw! yahoo\! google excite lycos ! # include yahoo! as an element</pre></blockquote><p>As in single-quoted strings, two consecutive backslashes contributeone single backslash to the item.</p><p>Now, although the Perl motto is "There's More Than OneWay To Do It," you may well wonder why anyone would need all ofthose different ways! Well, we'll see later that there areother kinds of quoting where Perl uses this same rule, and it cancome in handy in many of those. But even here, it could be useful ifyou were to need a list of Unix filenames:</p><blockquote><pre class="code">qw{  /usr/dict/words  /home/rootbeer/.ispell_english}</pre></blockquote><p>That list would be quite inconvenient to read, write, and maintain ifthe slash were the only delimiter available.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.2. Special Array Indices</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.4. List Assignment</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 + -