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

📄 ch04_03.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 4.2. Printing a List with Commas (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:31:11Z"><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_02.htm"TITLE="4.1. Specifying a List In Your Program"><LINKREL="next"HREF="ch04_04.htm"TITLE="4.3. Changing Array Size"></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_02.htm"TITLE="4.1. Specifying a List In Your Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.1. Specifying a List In Your Program"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_04.htm"TITLE="4.3. Changing Array Size"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.3. Changing Array Size"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch04-chap04_printing_0">4.2. Printing a List with Commas</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-173">Problem <ACLASS="indexterm"NAME="ch04-idx-1000006608-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006608-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006608-2"></A><ACLASS="indexterm"NAME="ch04-idx-1000006608-3"></A><ACLASS="indexterm"NAME="ch04-idx-1000006608-4"></A><ACLASS="indexterm"NAME="ch04-idx-1000006608-5"></A></A></H3><PCLASS="para">You'd like to print out a list with an unknown number of elements with an "and" before the last element, and with commas between each element if there are more than two.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-179">Solution</A></H3><PCLASS="para">Use this function, which returns the formatted string: <ACLASS="indexterm"NAME="ch04-idx-1000006616-0"></A></P><PRECLASS="programlisting">sub commify_series {    (@_ == 0) ? ''                                      :    (@_ == 1) ? $_[0]                                   :    (@_ == 2) ? join(&quot; and &quot;, @_)                       :                join(&quot;, &quot;, @_[0 .. ($#_-1)], &quot;and $_[-1]&quot;);}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-197">Discussion</A></H3><PCLASS="para">It often looks odd to print out arrays:</P><PRECLASS="programlisting">@array = (&quot;red&quot;, &quot;yellow&quot;, &quot;green&quot;);print &quot;I have &quot;, @array, &quot; marbles.\n&quot;;print &quot;I have @array marbles.\n&quot;;<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>I have redyellowgreen marbles.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>I have red yellow green marbles.</I></CODE></B></CODE></PRE><PCLASS="para">What you really want it to say is, <CODECLASS="literal">&quot;I</CODE> <CODECLASS="literal">have</CODE> <CODECLASS="literal">red,</CODE> <CODECLASS="literal">yellow,</CODE> <CODECLASS="literal">and</CODE> <CODECLASS="literal">green</CODE> <CODECLASS="literal">marbles&quot;</CODE>. The function given in the solution generates strings in that format. The word <CODECLASS="literal">&quot;and&quot;</CODE> is placed between the last two list elements. If there are more than two elements in the list, a comma is placed between every element.</P><PCLASS="para"><ACLASS="xref"HREF="ch04_03.htm#ch04-17462"TITLE="commify_series (continued)">Example 4.1</A> gives a complete demonstration of the function, with one addition: If any element in the list already contains a comma, a semi-colon is used for the separator character instead.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch04-17462">Example 4.1: commify_series (continued)</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# commify_series - show proper comma insertion in list output@lists = (    [ 'just one thing' ],    [ qw(Mutt Jeff) ],    [ qw(Peter Paul Mary) ],    [ 'To our parents', 'Mother Theresa', 'God' ],    [ 'pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna' ],    [ 'recycle tired, old phrases', 'ponder big, happy thoughts' ],    [ 'recycle tired, old phrases',       'ponder big, happy thoughts',       'sleep and dream peacefully' ],    );foreach $aref (@lists) {    print &quot;The list is: &quot; . commify_series(@$aref) . &quot;.\n&quot;;} sub commify_series {    my $sepchar = grep(/,/ =&gt; @_) ? &quot;;&quot; : &quot;,&quot;;    (@_ == 0) ? ''                                      :    (@_ == 1) ? $_[0]                                   :    (@_ == 2) ? join(&quot; and &quot;, @_)                       :                join(&quot;$sepchar &quot;, @_[0 .. ($#_-1)], &quot;and $_[-1]&quot;);}</PRE></DIV><PCLASS="para">Here's the output from the program:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: just one thing.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: Mutt and Jeff.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: Peter, Paul, and Mary.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: To our parents, Mother Theresa, and God.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: pastrami, ham and cheese, peanut butter and jelly, and tuna.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: recycle tired, old phrases and ponder big, happy thoughts.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>The list is: recycle tired, old phrases; ponder </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>   big, happy thoughts; and sleep and dream peacefully.</I></CODE></B></CODE></PRE><PCLASS="para">As you see, we don't follow the ill-advised practice of omitting the final comma from a series under any circumstances. To do so introduces unfortunate ambiguities and unjustifiable exceptions. The examples above would have claimed that we were the offspring of Mother Theresa and God, and would have had us eating sandwiches made of jelly and tuna fish fixed together atop the peanut butter.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch04-pgfId-289">See Also <ACLASS="indexterm"NAME="ch04-idx-1000006610-0"></A><ACLASS="indexterm"NAME="ch04-idx-1000006610-1"></A><ACLASS="indexterm"NAME="ch04-idx-1000006610-2"></A><ACLASS="indexterm"NAME="ch04-idx-1000006610-3"></A><ACLASS="indexterm"NAME="ch04-idx-1000006610-4"></A><ACLASS="indexterm"NAME="ch04-idx-1000006610-5"></A></A></H3><PCLASS="para">Fowler's <CITECLASS="citetitle">Modern English Usage</CITE>; we explain the nested list syntax in <ACLASS="xref"HREF="ch11_02.htm"TITLE="Taking References to Arrays">Recipe 11.1</A>; the <CODECLASS="literal">grep</CODE> function in <ICLASS="filename">perlfunc </I>(1) and <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the conditional operator <CODECLASS="literal">(&quot;?:&quot;)</CODE> is discussed in <ICLASS="filename">perlop </I>(1) and in the <ACLASS="olink"HREF="../prog/ch02_05.htm#PERL2-CH-2-SECT-5.16">"Conditional Operator"</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></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_02.htm"TITLE="4.1. Specifying a List In Your Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 4.1. Specifying a List In Your Program"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_04.htm"TITLE="4.3. Changing Array Size"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 4.3. Changing Array Size"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">4.1. Specifying a List In Your Program</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.3. Changing Array Size</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 &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 + -