ch05_04.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 143 行
HTM
143 行
<html><head><title>Typical Use of a Hash (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="ch05_03.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="ch05_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">5.4. Typical Use of a Hash</h2><p>At this point, you may find it helpful to see a more concreteexample.</p><p>The Bedrock library uses a Perl program in which a hash keeps trackof how many books each person has checked out, among otherinformation:</p><blockquote><pre class="code">$books{"fred"} = 3;$books{"wilma"} = 1;</pre></blockquote><p>It's easy to see whether an element of the hash is true orfalse, do this:</p><blockquote><pre class="code">if ($books{$someone}) { print "$someone has at least one book checked out.\n";}</pre></blockquote><p>But there are some elements of the hash that aren't true:</p><blockquote><pre class="code">$books{"barney"} = 0; # no books currently checked out$books{"pebbles"} = undef; # no books EVER checked out - a new library card</pre></blockquote><p>Since Pebbles has never checked out any books, her entry has thevalue of <tt class="literal">undef</tt>, rather than <tt class="literal">0</tt>.</p><p>There's a key in the hash for everyone who has a library card.For each key (that is, for each library patron), there's avalue that is either a number of books checked out, or<tt class="literal">undef</tt> if that person's library card hasnever been used.</p><a name="lperl3-CHP-5-SECT-4.1" /><div class="sect2"><h3 class="sect2">5.4.1. The exists Function</h3><p>To see whether a key <a name="INDEX-411" /> <a name="INDEX-412" />exists in the hash, (that is, whethersomeone has a library card or not), use the<tt class="literal">exists</tt> function, which returns a true value ifthe given key exists in the hash, whether the corresponding value istrue or not:</p><blockquote><pre class="code">if (exists $books{"dino"}) { print "Hey, there's a library card for dino!\n";}</pre></blockquote><p>That is to say, <tt class="literal">exists $books{"dino"}</tt> will returna true value if (and only if) <tt class="literal">dino</tt> is found in thelist of keys from <tt class="literal">keys %books</tt>.</p></div><a name="lperl3-CHP-5-SECT-4.2" /><div class="sect2"><h3 class="sect2">5.4.2. The delete Function</h3><p>The <tt class="literal">delete</tt><a name="INDEX-413" /> <a name="INDEX-414" /> function removes the given key (andits corresponding value) from the hash. (If there's no suchkey, its work is done; there's no warning or error in thatcase.)</p><blockquote><pre class="code">my $person = "betty";delete $books{$person}; # Revoke the library card for $person</pre></blockquote><p>Note that this is <em class="emphasis">not</em> the same as storing<tt class="literal">undef</tt> into that hash element -- in fact,it's precisely the opposite! Checking<tt class="literal">exists($books{"betty"})</tt> will give opposite resultsin these two cases; after a <tt class="literal">delete</tt>, the key<em class="emphasis">can't</em> exist in the hash, but after storing<tt class="literal">undef</tt>, the key <em class="emphasis">must</em> exist.</p></div><a name="lperl3-CHP-5-SECT-4.3" /><div class="sect2"><h3 class="sect2">5.4.3. Hash Element Interpolation</h3><p>You can interpolate a single<a name="INDEX-415" /> <a name="INDEX-416" />hash element into a double-quoted stringjust as you'd expect:</p><blockquote><pre class="code">foreach $person (sort keys %books) { # for each library patron,in order if ($books{$person}) { print "$person has $books{$person} items\n";# fred has 3 items }}</pre></blockquote><p>But there's no support for entire hash interpolation;<tt class="literal">"%books"</tt> is just the six chararcters of(literally) <tt class="literal">%books</tt>.<a href="#FOOTNOTE-136">[136]</a> So we've seen all of themagical characters that need <a name="INDEX-417" />backslashing in double quotes:<tt class="literal">$</tt> and <tt class="literal">@</tt>, because they introducea variable to be interpolated; <tt class="literal">"</tt>, sincethat's the quoting character that would otherwise end thedouble-quoted string; and <tt class="literal">\</tt>, the backslash itself.Any other characters in a double-quoted string are non-magical andshould simply stand for themselves.<a href="#FOOTNOTE-137">[137]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-136" /><p>[136]Well, itcouldn't really be anything else; if we tried to print out theentire hash, as a series of key-value pairs, that would be nearlyuseless. And, as we'll see in <a href="ch06_01.htm">Chapter 6, "I/O Basics"</a>, thepercent sign is frequently used in <tt class="literal">printf </tt>formatstrings; giving it another meaning here would be terriblyinconvenient.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-137" /><p>[137]But do beware ofthe apostrophe (<tt class="literal">'</tt>), left square bracket(<tt class="literal">[</tt>), left curly brace (<tt class="literal">{</tt>), thesmall arrow (<tt class="literal">-></tt>), or double-colon(<tt class="literal">::</tt>) following a variable name in a double-quotedstring, as they could perhaps mean something you didn'tintend.</p> </blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch05_03.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="ch05_05.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">5.3. Hash Functions</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">5.5. 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 + =
减小字号Ctrl + -
显示快捷键?