📄 ch04_02.htm
字号:
<HTML><HEAD><TITLE>Recipe 4.1. Specifying a List In Your Program (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:31:01Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch04_01.htm"TITLE="4. Arrays"><LINKREL="prev"HREF="ch04_01.htm"TITLE="4.0. Introduction"><LINKREL="next"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas"></HEAD><BODYBGCOLOR="#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="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_01.htm"TITLE="4.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch04_01.htm"TITLE="4. Arrays"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.2. Printing a List with Commas"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch04-chap04_specifying_0">4.1. Specifying a List In Your Program</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-63">Problem <ACLASS="indexterm"NAME="ch04-idx-1000006572-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006572-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006572-2"></A></A></H3><PCLASS="para">You want to include a list in your program. This is how you initialize arrays.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-69">Solution</A></H3><PCLASS="para">You can write out a <ACLASS="indexterm"NAME="ch04-idx-1000006588-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006588-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006588-2"></A>comma-separated list of elements:</P><PRECLASS="programlisting">@a = ("quick", "brown", "fox");</PRE><PCLASS="para">If you have a lot of single-word elements, use the <CODECLASS="literal">qw()</CODE><ACLASS="indexterm"NAME="ch04-idx-1000006590-0"></A> operator:</P><PRECLASS="programlisting">@a = qw(Why are you teasing me?);</PRE><PCLASS="para">If you have a lot of multi-word elements, use a here document and extract lines:</P><PRECLASS="programlisting">@lines = (<<"END_OF_HERE_DOC" =~ m/^\s*(.+)/gm); The boy stood on the burning deck, It was as hot as glass.END_OF_HERE_DOC</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-91">Discussion</A></H3><PCLASS="para">The first technique is the most commonly used, often because only small arrays are normally initialized as program literals. Initializing a large array would fill your program with values and make it hard to read, so such arrays are either initialized in a separate library file (see <ACLASS="xref"HREF="ch12_01.htm"TITLE="Packages, Libraries, and Modules">Chapter 12, <CITECLASS="chapter">Packages, Libraries, and Modules</CITE></A>), or the values are simply read from a file:</P><PRECLASS="programlisting">@bigarray = ();open(DATA, "< mydatafile") or die "Couldn't read from datafile: $!\n";while (<DATA>) { chomp; push(@bigarray, $_);}</PRE><PCLASS="para">The second technique uses the <CODECLASS="literal">qw()</CODE> operator, one of the quoting operators. Along with <CODECLASS="literal">q()</CODE><ACLASS="indexterm"NAME="ch04-idx-1000006593-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006593-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006593-2"></A><ACLASS="indexterm"NAME="ch04-idx-1000006593-3"></A>, <CODECLASS="literal">qq()</CODE>, and <CODECLASS="literal">qx()</CODE>, <CODECLASS="literal">qw()</CODE> <ACLASS="indexterm"NAME="ch04-idx-1000006598-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006598-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006598-2"></A><ACLASS="indexterm"NAME="ch04-idx-1000006598-3"></A>provides another way to quote values for your program. <CODECLASS="literal">q()</CODE> behaves like single quotes, so these two lines are equivalent:</P><PRECLASS="programlisting">$banner = 'The Mines of Moria';$banner = q(The Mines of Moria);</PRE><PCLASS="para">Similarly, <CODECLASS="literal">qq()</CODE> behaves like double quotes:</P><PRECLASS="programlisting">$name = "Gandalf";$banner = "Speak, $name, and enter!";$banner = qq(Speak, $name, and welcome!);</PRE><PCLASS="para"><ACLASS="indexterm"NAME="ch04-idx-1000006600-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006600-1"></A>And <CODECLASS="literal">qx()</CODE> is almost exactly like backticks; that is, it runs a command through the shell complete with variable interpolation and backslash escapes. You can't stop interpolation with backticks, but you can with <CODECLASS="literal">qx</CODE>. If you don't want Perl variables to be expanded, you can use a single-quote delimiter on <CODECLASS="literal">qx</CODE> to suppress this:</P><PRECLASS="programlisting">$his_host = 'www.perl.com';$host_info = `nslookup $his_host`; # expand Perl variable$perl_info = qx(ps $$); # that's Perl's $$$shell_info = qx'ps $$'; # that's the new shell's $$</PRE><PCLASS="para">Whereas <CODECLASS="literal">q()</CODE>, <CODECLASS="literal">qq()</CODE>, and <CODECLASS="literal">qx()</CODE> quote single strings, <CODECLASS="literal">qw()</CODE> quotes a list of single-word strings by splitting its argument on whitespace, without variable interpolation. These are all the same:</P><PRECLASS="programlisting">@banner = ('Costs', 'only', '$4.95');@banner = qw(Costs only $4.95);@banner = split(' ', 'Costs only $4.95');</PRE><PCLASS="para">All quoting operators behave like regular expression matches, in that you can select your quote delimiters, including paired brackets. All four kinds of brackets (angle, square, curly, and round ones) nest properly. That means you can easily use parentheses or braces (or the other two) without fear, provided that they match up:</P><PRECLASS="programlisting">@brax = qw! ( ) < > { } [ ] !;@rings = qw(Nenya Narya Vilya);@tags = qw<LI TABLE TR TD A IMG H1 P>;@sample = qw(The vertical bar (|) looks and behaves like a pipe.);</PRE><PCLASS="para">If you don't want to change the quoting character, use a backslash to escape the delimiter in the string:</P><PRECLASS="programlisting">@banner = qw|The vertical bar (\|) looks and behaves like a pipe.|;</PRE><PCLASS="para">You may only use <CODECLASS="literal">qw()</CODE> when each separate, space-separated component is an element in the return list. Be careful not to give Columbus four ships instead of three:</P><PRECLASS="programlisting">@ships = qw(Ni馻 Pinta Santa Mar韆); # WRONG@ships = ('Ni馻', 'Pinta', 'Santa Mar韆'); # right</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-163">See Also</A></H3><PCLASS="para">The "List Value Constructors" section of <ICLASS="filename">perldata </I>(1); the <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.4">"List Values and Arrays"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the "Quote and Quote-Like Operators" section of <ICLASS="filename">perlop </I>(1); the <CODECLASS="literal">s///</CODE> operator in <ICLASS="filename">perlop </I>(1) and <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A><ACLASS="indexterm"NAME="ch04-idx-1000006580-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006580-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006580-2"></A><ACLASS="indexterm"NAME="ch04-idx-1000006580-3"></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_01.htm"TITLE="4.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch04_03.htm"TITLE="4.2. Printing a List with Commas"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.2. Printing a List with Commas"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">4.0. Introduction</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">4.2. Printing a List with Commas</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -