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

📄 ch02_19.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 2.18. Printing Correct Plurals (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:30:25Z"><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="ch02_01.htm"TITLE="2. Numbers"><LINKREL="prev"HREF="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers"><LINKREL="next"HREF="ch02_20.htm"TITLE="2.19. Program: Calculating Prime Factors"></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="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.17. Putting Commas in Numbers"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch02_01.htm"TITLE="2. Numbers"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch02_20.htm"TITLE="2.19. Program: Calculating Prime Factors"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.19. Program: Calculating Prime Factors"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch02-chap02_printing_0">2.18. Printing Correct Plurals</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1349">Problem <ACLASS="indexterm"NAME="ch02-idx-1000008154-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008154-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008154-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008154-3"></A></A></H3><PCLASS="para">You're printing something like &quot;<CODECLASS="literal">It</CODE> <CODECLASS="literal">took</CODE> <CODECLASS="literal">$time</CODE> <CODECLASS="literal">hours</CODE>&quot;, but &quot;<CODECLASS="literal">It</CODE> <CODECLASS="literal">took</CODE> <CODECLASS="literal">1</CODE> <CODECLASS="literal">hours</CODE>&quot; is ungrammatical. You would like to get it right.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1355">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">printf</CODE> and a ternary conditional (<CODECLASS="literal">X</CODE> <CODECLASS="literal">?</CODE> <CODECLASS="literal">Y</CODE> <CODECLASS="literal">:</CODE> <CODECLASS="literal">Z</CODE>) to alter the noun or verb:</P><PRECLASS="programlisting">printf &quot;It took %d hour%s\n&quot;, $time, $time == 1 ? &quot;&quot; : &quot;s&quot;;printf &quot;%d hour%s %s enough.\n&quot;, $time,         $time == 1 ? &quot;&quot;   : &quot;s&quot;,        $time == 1 ? &quot;is&quot; : &quot;are&quot;;</PRE><PCLASS="para">Or, use the <ACLASS="indexterm"NAME="ch02-idx-1000008155-0"></A>Lingua::EN::Inflect module from CPAN as described in the Discussion.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1373">Discussion</A></H3><PCLASS="para">The only reason inane messages like &quot;<CODECLASS="literal">1</CODE> <CODECLASS="literal">file(s)</CODE> <CODECLASS="literal">updated</CODE>&quot; appear is because their authors are too lazy to bother checking whether the count is 1 or not.</P><PCLASS="para">If your noun changes by more than an <CODECLASS="literal">&quot;-s&quot;</CODE>, you'll need to change the <CODECLASS="literal">printf</CODE> accordingly:</P><PRECLASS="programlisting">printf &quot;It took %d centur%s&quot;, $time, $time == 1 ? &quot;y&quot; : &quot;ies&quot;;</PRE><PCLASS="para">This is good for simple cases, but you'll get tired of writing it. This leads you to write funny functions like this:</P><PRECLASS="programlisting">sub noun_plural {    local $_ = shift;    # order really matters here!    s/ss$/sses/                             ||    s/([psc]h)$/${1}es/                     ||    s/z$/zes/                               ||    s/ff$/ffs/                              ||    s/f$/ves/                               ||    s/ey$/eys/                              ||    s/y$/ies/                               ||    s/ix$/ices/                             ||    s/([sx])$/$1es/                         ||    s/$/s/                                  ||                die &quot;can't get here&quot;;    return $_;}*verb_singular = \&amp;noun_plural;   # make function alias</PRE><PCLASS="para">As you find more exceptions, your function will become increasingly convoluted. When you need to handle such morphological changes, turn to the flexible solution provided by the Lingua::EN::Inflect module from CPAN.</P><PRECLASS="programlisting">use Lingua::EN::Inflect qw(PL classical);classical(1);               # why isn't this the default?while (&lt;DATA&gt;) {            # each line in the data    for (split) {           # each word on the line        print &quot;One $_, two &quot;, PL($_), &quot;.\n&quot;;    }} # plus one more$_ = 'secretary general';print &quot;One $_, two &quot;, PL($_), &quot;.\n&quot;;__END__fish fly ox species genus phylum cherub radius jockey index matrix mythosphenomenon formula </PRE><PCLASS="para">That produces the following:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One fish, two fish.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One fly, two flies.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One ox, two oxen.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One species, two species.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One genus, two genera.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One phylum, two phyla.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One cherub, two cherubim.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One radius, two radii.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One jockey, two jockeys.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One index, two indices.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One matrix, two matrices.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One mythos, two mythoi.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One phenomenon, two phenomena.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One formula, two formulae.</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>One secretary general, two secretaries general.</I></CODE></B></CODE></PRE><PCLASS="para">This is one of the many things the module can do. It also handles inflections or conjugations for other parts of speech, provides number-insensitive comparison functions, figures out whether to use <ICLASS="firstterm">a</I> or <ICLASS="firstterm">an</I>, and plenty more.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch02-pgfId-1489">See Also</A></H3><PCLASS="para">The ternary ("hook-colon") operator discussed in <EMCLASS="emphasis">perlop </EM>(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>; the documentation with the CPAN module Lingua::EN::Inflect<ACLASS="indexterm"NAME="ch02-idx-1000008156-0"></A> <ACLASS="indexterm"NAME="ch02-idx-1000008158-0"></A><ACLASS="indexterm"NAME="ch02-idx-1000008158-1"></A><ACLASS="indexterm"NAME="ch02-idx-1000008158-2"></A><ACLASS="indexterm"NAME="ch02-idx-1000008158-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="ch02_18.htm"TITLE="2.17. Putting Commas in Numbers"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 2.17. Putting Commas in Numbers"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="ch02_20.htm"TITLE="2.19. Program: Calculating Prime Factors"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 2.19. Program: Calculating Prime Factors"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">2.17. Putting Commas in Numbers</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">2.19. Program: Calculating Prime Factors</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 + -