📄 ch05_02.htm
字号:
<HTML><HEAD><TITLE>Recipe 5.1. Adding an Element to a Hash (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:32:28Z"><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="ch05_01.htm"TITLE="5. Hashes"><LINKREL="prev"HREF="ch05_01.htm"TITLE="5.0. Introduction"><LINKREL="next"HREF="ch05_03.htm"TITLE="5.2. Testing for the Presence of a Key in a Hash"></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="ch05_01.htm"TITLE="5.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 5.0. Introduction"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch05_01.htm"TITLE="5. Hashes"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch05_03.htm"TITLE="5.2. Testing for the Presence of a Key in a Hash"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 5.2. Testing for the Presence of a Key in a Hash"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch05-chap05_adding_0">5.1. Adding an Element to a Hash</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-70">Problem <ACLASS="indexterm"NAME="ch05-idx-1000006422-0"></A><ACLASS="indexterm"NAME="ch05-idx-1000006422-1"></A></A></H3><PCLASS="para">You need to add an entry to a hash.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-76">Solution</A></H3><PCLASS="para">Simply assign to the hash key:</P><PRECLASS="programlisting">$HASH{$KEY} = $VALUE;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-84">Discussion</A></H3><PCLASS="para">Putting something into a hash is straightforward. In languages that don't provide the hash as an intrinsic data type, you have to worry about overflows, resizing, and collisions in your hash table. In Perl, all that is taken care of for you with a simple assignment. If that entry was already occupied (had a previous value), memory for that value is automatically freed, just as when assigning to a simple scalar.</P><PRECLASS="programlisting"># %food_color defined per the introduction$food_color{Raspberry} = "pink";print "Known foods:\n";foreach $food (keys %food_color) { print "$food\n";}<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Known foods:</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Banana</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Apple</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Raspberry</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Carrot</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Lemon</I></CODE></B></CODE></PRE><PCLASS="para">If you store <CODECLASS="literal">undef</CODE> as a hash key, it gets stringified to <CODECLASS="literal">""</CODE> (and generates a warning if your program is running under <BCLASS="emphasis.bold">-w</B>). Using <CODECLASS="literal">undef</CODE> as a key is probably not what you want. On the other hand, <CODECLASS="literal">undef</CODE> is a valid <EMCLASS="emphasis">value</EM> in a hash. But if you fetch the value for a key that isn't in the hash, you'll also get <CODECLASS="literal">undef</CODE>. This means you can't use the simple Boolean test <CODECLASS="literal">if</CODE> <CODECLASS="literal">($hash{$key})</CODE> to see whether there is an entry in <CODECLASS="literal">%hash</CODE> for <CODECLASS="literal">$key</CODE>. Use <CODECLASS="literal">exists($hash{$key})</CODE><ACLASS="indexterm"NAME="ch05-idx-1000006424-0"></A> to test whether a key is in the hash, <CODECLASS="literal">defined($hash{$key})</CODE> to test if the corresponding value is not <CODECLASS="literal">undef</CODE>, and <CODECLASS="literal">if</CODE> <CODECLASS="literal">($hash{$key})</CODE> to test if the corresponding value is a true value.</P><PCLASS="para">In Perl's hashing algorithm, permutations of a string hash to the same spot internally. If your hash contains as keys many permutations of the same string, like <CODECLASS="literal">"sparc"</CODE> and <CODECLASS="literal">"craps"</CODE>, hash performance can degrade noticeably. In practice, this seldom occurs.<ACLASS="indexterm"NAME="ch05-idx-1000006423-0"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-120">See Also</A></H3><PCLASS="para">The "List Value Constructors" section of <ICLASS="filename">perldata </I>(1); the <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.4">"List Values and Arrays"</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>; <ACLASS="xref"HREF="ch05_03.htm"TITLE="Testing for the Presence of a Key in a Hash">Recipe 5.2</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="ch05_01.htm"TITLE="5.0. Introduction"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 5.0. Introduction"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="ch05_03.htm"TITLE="5.2. Testing for the Presence of a Key in a Hash"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 5.2. Testing for the Presence of a Key in a Hash"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">5.0. Introduction</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">5.2. Testing for the Presence of a Key in a Hash</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 © 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 + -