📄 ch06_05.htm
字号:
<html><head><title>Formatted Output with printf (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 & 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="ch06_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="ch06_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">6.5. Formatted Output with printf</h2><p><a name="INDEX-467" /> <a name="INDEX-468" /> <a name="INDEX-469" />You may wish to have a little morecontrol with your output than <tt class="literal">print</tt> provides. Infact, you may be accustomed to the formatted output of C's<tt class="literal">printf</tt> function. Fear not -- Perl provides acomparable operation with the same name.</p><p>The <tt class="literal">printf</tt> operator takes a format stringfollowed by a list of things to print. The format<a href="#FOOTNOTE-158">[158]</a> string is a fill-in-the-blanks templateshowing the desired form of the output:</p><blockquote class="footnote"><a name="FOOTNOTE-158" /><p>[158]Here, we're using "format" in the genericsense. Perl has a report-generating feature called"formats" that we won't even be mentioning (exceptin this footnote) until <a href="appb_01.htm">Appendix B, "Beyond the Llama"</a>, and then onlyto say that we really aren't going to talk about them. So,you're on your own there. Just wanted to keep you from gettinglost.</p> </blockquote><blockquote><pre class="code">printf "Hello, %s; your password expires in %d days!\n", $user, $days_to_die;</pre></blockquote><p>The format string holds a number of so-called<em class="firstterm">conversions</em><a name="INDEX-470" /><a name="INDEX-471" /> <a name="INDEX-472" /><a name="INDEX-473" />; each conversionbegins with a percent sign (<tt class="literal">%</tt>) and ends with aletter. (As we'll see in a moment, there may be significantextra characters between these two symbols.) There should be the samenumber of items in the following list as there are conversions; ifthese don't match up, it won't work correctly. In theexample above, there are two items and two conversions, so the outputmight look something like this:</p><blockquote><pre class="code">Hello, merlyn; your password expires in 3 days!</pre></blockquote><p>There are many possible <tt class="literal">printf</tt> conversions, sowe'll take time here to describe just the most common ones. Ofcourse, the full details are available in the<tt class="literal">perlfunc</tt> manpage.</p><p>To print a number in what's generally a good way, use<tt class="literal">%g</tt><a name="INDEX-474" /> <a name="INDEX-475" />,<a href="#FOOTNOTE-159">[159]</a> whichautomatically chooses floating-point, integer, or even exponentialnotation as needed:</p><blockquote class="footnote"><a name="FOOTNOTE-159" /><p>[159]"General" numeric conversion. Or maybe a"Good conversion for this number" or "Guess what Iwant the output to look like."</p> </blockquote><blockquote><pre class="code">printf "%g %g %g\n", 5/2, 51/17, 51 ** 17; # 2.5 3 1.0683e+29</pre></blockquote><p>The <tt class="literal">%d</tt><a name="INDEX-476" /> <a name="INDEX-477" /><a name="INDEX-478" /> format means a decimal<a href="#FOOTNOTE-160">[160]</a> integer, truncated as needed:</p><blockquote class="footnote"><a name="FOOTNOTE-160" /><p>[160]There's also <tt class="literal">%x</tt> for hexadecimal and<tt class="literal">%o</tt> for octal, if you need those. But we really say"decimal" here as a memory aid: <tt class="literal">%d</tt> forDecimal integer.</p> </blockquote><blockquote><pre class="code">printf "in %d days!\n", 17.85; # in 17 days!</pre></blockquote><p>Note that this is truncated, not rounded; we'll see how toround off a number in a moment.</p><p>In Perl, <tt class="literal">printf</tt> is most often used for columnardata, since most formats accept a field width. If the datawon't fit, the field will generally be expanded as needed:</p><blockquote><pre class="code">printf "%6d\n", 42; # output like ‘‘‘‘42 (the ‘ symbol stands for a space)printf "%2d\n", 2e3 + 1.95; # 2001</pre></blockquote><p>The <tt class="literal">%s</tt><a name="INDEX-479" /> conversion means a string, so iteffectively interpolates the given value as a string, but with agiven field width:</p><blockquote><pre class="code">printf "%10s\n", "wilma"; # looks like ‘‘‘‘‘wilma</pre></blockquote><p>A negative field width is left-justified (in any of theseconversions):</p><blockquote><pre class="code">printf "%-15s\n", "flintstone"; # looks like flintstone ‘‘‘‘‘</pre></blockquote><p>The <tt class="literal">%f</tt><a name="INDEX-480" /> <a name="INDEX-481" /> conversion (floating-point) roundsoff its output as needed, and even lets you request a certain numberof digits after the decimal point:</p><blockquote><pre class="code">printf "%12f\n", 6 * 7 + 2/3; # looks like ‘‘‘42.666667printf "%12.3f\n", 6 * 7 + 2/3; # looks like ‘‘‘‘‘‘42.667printf "%12.0f\n", 6 * 7 + 2/3; # looks like ‘‘‘‘‘‘‘‘‘‘43</pre></blockquote><p>To print a real <a name="INDEX-482" />percent sign,use <tt class="literal">%%</tt>, which is special in that it uses noelement from the list:<a href="#FOOTNOTE-161">[161]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-161" /><p>[161]Maybe you thought you couldsimply put a backslash in front of the percent sign. Nice try, butno. The reason that won't work is that the format is an<em class="emphasis">expression</em>, and the expression <tt class="literal">"\%"</tt>means the one-character string <tt class="literal">'%'</tt>. Evenif we got a backslash into the format string, <tt class="literal">printf</tt>wouldn't know what to do with it. Besides, Cprogrammers are used to <tt class="literal">printf </tt>working likethis.</p> </blockquote><blockquote><pre class="code">printf "Monthly interest rate: %.2f%%\n", 5.25/12; # the value looks like "0.44%"</pre></blockquote><a name="lperl3-CHP-6-SECT-5.1" /><div class="sect2"><h3 class="sect2">6.5.1. Arrays and printf</h3><p>Generally, you won't use an<a name="INDEX-483" />array as an argument to<tt class="literal">printf</tt>. That's because an array may holdany number of items, and a given format string will work with only acertain fixed number of items: if there are three conversions in theformat, there must be exactly three items.</p><p>But there's no reason you can't whip up a format stringon the fly, since it may be any expression. This can be tricky to getright, though, so it may be handy (especially when debugging) tostore the format into a variable:</p><blockquote><pre class="code">my @items = qw( wilma dino pebbles );my $format = "The items are:\n" . ("%10s\n" x @items);## print "the format is <<$format>>\n"; # for debuggingprintf $format, @items;</pre></blockquote><p>This uses the <a name="INDEX-484" /> <a name="INDEX-485" /><tt class="literal">x</tt>operator (which we learned about in <a href="ch02_01.htm">Chapter 2, "Scalar Data"</a>) toreplicate the given string a number of times given by<tt class="literal">@items</tt> (which is being used in a scalar context).In this case, that's <tt class="literal">3</tt>, since there arethree items, so the resulting format string is the same as if we hadwritten it as <tt class="literal">"The itemsare:\n%10s\n%10s\n%10s\n</tt>.<tt class="literal">"</tt> And theoutput prints each item on its own line, right-justified in aten-character column, under a heading line. Pretty cool, huh? But notcool enough, because you can even combine these:</p><blockquote><pre class="code">printf "The items are:\n".("%10s\n" x @items), @items;</pre></blockquote><p>Note that here we have <tt class="literal">@items</tt> being used once in ascalar context, to get its length, and once in a list context, to getits contents. Context is<a name="INDEX-486" /> <a name="INDEX-487" /> <a name="INDEX-488" /> important.<a name="INDEX-489" /></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="ch06_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="ch06_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">6.4. Output to Standard Output</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">6.6. Exercises</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 © 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 + -