📄 ch05_04.htm
字号:
<HTML><HEAD><TITLE>Recipe 5.3. Deleting from 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:32Z"><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_03.htm"TITLE="5.2. Testing for the Presence of a Key in a Hash"><LINKREL="next"HREF="ch05_05.htm"TITLE="5.4. Traversing 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_03.htm"TITLE="5.2. Testing for the Presence of a Key in a Hash"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 5.2. Testing for the Presence of a Key in a Hash"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_05.htm"TITLE="5.4. Traversing a Hash"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 5.4. Traversing a Hash"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch05-chap05_deleting_0">5.3. Deleting from a Hash</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-260">Problem<ACLASS="indexterm"NAME="ch05-idx-1000006427-0"></A><ACLASS="indexterm"NAME="ch05-idx-1000006427-1"></A></A></H3><PCLASS="para">You want to remove an entry from a hash so that it doesn't show up with <CODECLASS="literal">keys</CODE>, <CODECLASS="literal">values</CODE>, or <CODECLASS="literal">each</CODE>. If you were using a hash to associate salaries with employees, and an employee resigned, you'd want to remove their entry from the hash.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-266">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">delete</CODE><ACLASS="indexterm"NAME="ch05-idx-1000006437-0"></A> function:</P><PRECLASS="programlisting"># remove $KEY and its value from %HASHdelete($HASH{$KEY});</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-276">Discussion</A></H3><PCLASS="para">Sometimes people mistakenly try to use <CODECLASS="literal">undef</CODE> to remove an entry from a hash. <CODECLASS="literal">undef</CODE> <CODECLASS="literal">$hash{$key}</CODE> and <CODECLASS="literal">$hash{$key}</CODE> <CODECLASS="literal">=</CODE> <CODECLASS="literal">undef</CODE> both make <CODECLASS="literal">%hash</CODE> have an entry with key <CODECLASS="literal">$key</CODE> and value <CODECLASS="literal">undef</CODE>.</P><PCLASS="para">The <CODECLASS="literal">delete</CODE> function is the only way to remove a specific entry from a hash. Once you've deleted a key, it no longer shows up in a <CODECLASS="literal">keys</CODE> list or an <CODECLASS="literal">each</CODE> iteration, and <CODECLASS="literal">exists</CODE> will return false for that key.</P><PCLASS="para">This demonstrates the difference between <CODECLASS="literal">undef</CODE> and <CODECLASS="literal">delete</CODE>:</P><PRECLASS="programlisting"># %food_color as per Introductionsub print_foods { my @foods = keys %food_color; my $food; print "Keys: @foods\n"; print "Values: "; foreach $food (@foods) { my $color = $food_color{$food}; if (defined $color) { print "$color "; } else { print "(undef) "; } } print "\n";}print "Initially:\n";<CODECLASS="literal">print_foods();</CODE>print "\nWith Banana undef\n";undef $food_color{"Banana"};<CODECLASS="literal">print_foods();</CODE>print "\nWith Banana deleted\n";delete $food_color{"Banana"};<CODECLASS="literal">print_foods();</CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Initially:</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Keys: Banana Apple Carrot Lemon</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Values: yellow red orange yellow </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>With Banana undef</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Keys: Banana Apple Carrot Lemon</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Values: (undef) red orange yellow </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>With Banana deleted</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Keys: Apple Carrot Lemon</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Values: red orange yellow </I></CODE></B></CODE></PRE><PCLASS="para">As you see, if we set <CODECLASS="literal">$food_color{"Banana"}</CODE> to <CODECLASS="literal">undef</CODE>, <CODECLASS="literal">"Banana"</CODE> still shows up as a key in the hash. The entry is still there; we only succeeded in making the value <CODECLASS="literal">undef</CODE>. On the other hand, <CODECLASS="literal">delete</CODE> actually removed it from the hash - <CODECLASS="literal">"Banana"</CODE> is no longer in the list returned by <CODECLASS="literal">keys</CODE>.</P><PCLASS="para"><CODECLASS="literal">delete</CODE> can also take a hash slice, deleting all listed keys at once:</P><PRECLASS="programlisting">delete @food_color{"Banana", "Apple", "Cabbage"};</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch05-pgfId-376">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">delete</CODE> and <CODECLASS="literal">keys</CODE> functions in <ICLASS="filename">perlfunc </I>(1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; we use keys in <ACLASS="xref"HREF="ch05_05.htm"TITLE="Traversing a Hash">Recipe 5.4</A><ACLASS="indexterm"NAME="ch05-idx-1000006429-0"></A><ACLASS="indexterm"NAME="ch05-idx-1000006429-1"></A><ACLASS="indexterm"NAME="ch05-idx-1000006429-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_03.htm"TITLE="5.2. Testing for the Presence of a Key in a Hash"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 5.2. Testing for the Presence of a Key in a Hash"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_05.htm"TITLE="5.4. Traversing a Hash"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 5.4. Traversing a Hash"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">5.2. Testing for the Presence of a Key in a Hash</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.4. Traversing 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 + -