📄 ch17_03.htm
字号:
<html><head><title>Transforming Items from a List with map (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="ch17_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="ch17_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">17.3. Transforming Items from a List with map</h2><p>Another common task is transforming items from a list. For example,suppose you have a list of numbers that should be formatted as<a name="INDEX-1101" />"moneynumbers" for<a name="INDEX-1102" />output, as with the subroutine<tt class="literal">&big_money</tt> (from <a href="ch15_01.htm">Chapter 15, "Strings and Sorting"</a>). But we don't want to modify theoriginal data; we need a modified copy of the list just for output.Here's one way to do that:</p><blockquote><pre class="code">my @data = (4.75, 1.5, 2, 1234, 6.9456, 12345678.9, 29.95);my @formatted_data;foreach (@data) { push @formatted_data, &big_money($_);}</pre></blockquote><p>That looks similar in form to the example code used at the beginningof the section on <tt class="literal">grep</tt>, doesn't it? So itmay not surprise you that the replacement code resembles the first<tt class="literal">grep</tt> example:</p><blockquote><pre class="code">my @data = (4.75, 1.5, 2, 1234, 6.9456, 12345678.9, 29.95);my @formatted_data = map { &big_money($_) } @data;</pre></blockquote><p>The <tt class="literal">map</tt><a name="INDEX-1103" /> <a name="INDEX-1104" /> operator looks much like<tt class="literal">grep</tt> because it has the same kind of arguments: ablock that uses <tt class="literal">$_</tt>, and a list of items toprocess. And it operates in a similar way, evaluating the block oncefor each item in the list, with <tt class="literal">$_</tt> aliased to adifferent original list element each time. But the last expression ofthe block is used differently; instead of giving a Boolean value, thefinal value actually becomes part of the resulting list.<a href="#FOOTNOTE-363">[363]</a></p><blockquote class="footnote"><a name="FOOTNOTE-363" /><p>[363]One other important difference is that the expression used by<tt class="literal">map</tt> is evaluated in a list context and may returnany number of items, not necessarily one each time.</p></blockquote><p>Any <tt class="literal">grep</tt> or <tt class="literal">map</tt> statement couldbe rewritten as a <tt class="literal">foreach</tt> loop pushing items ontoa temporary array. But the shorter way is typically more efficientand more convenient. Since the result of <tt class="literal">map</tt> or<tt class="literal">grep</tt> is a list, it can be passed directly toanother function. Here we can print that list of formatted"money numbers" as an indented list under a heading:</p><blockquote><pre class="code">print "The money numbers are:\n", map { sprintf("%25s\n", $_) } @formatted_data;</pre></blockquote><p>Of course, we could have done that processing all at once, withouteven the temporary array <tt class="literal">@formatted_data</tt>:</p><blockquote><pre class="code">my @data = (4.75, 1.5, 2, 1234, 6.9456, 12345678.9, 29.95);print "The money numbers are:\n", map { sprintf("%25s\n", &big_money($_) ) } @data;</pre></blockquote><p>As we saw with <tt class="literal">grep</tt>, there's also a simplersyntax for <tt class="literal">map</tt>. If all you need for the selectoris a simple expression (rather than a whole block), you can just usethat expression, followed by a comma, in place of the block:</p><blockquote><pre class="code">print "Some powers of two are:\n", map "\t" . ( 2 ** $_ ) . "\n", 0..15;</pre></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch17_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="ch17_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">17.2. Picking Items from a List with grep</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">17.4. Unquoted Hash Keys</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 + -