📄 ch11_03.htm
字号:
<HTML><HEAD><TITLE>Recipe 11.2. Making Hashes of Arrays (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:40: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="ch11_01.htm"TITLE="11. References and Records"><LINKREL="prev"HREF="ch11_02.htm"TITLE="11.1. Taking References to Arrays"><LINKREL="next"HREF="ch11_04.htm"TITLE="11.3. Taking References to Hashes"></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="ch11_02.htm"TITLE="11.1. Taking References to Arrays"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 11.1. Taking References to Arrays"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch11_01.htm"TITLE="11. References and Records"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch11_04.htm"TITLE="11.3. Taking References to Hashes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.3. Taking References to Hashes"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch11-chap11_making_0">11.2. Making Hashes of Arrays</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-332">Problem<ACLASS="indexterm"NAME="ch11-idx-1000004802-0"></A><ACLASS="indexterm"NAME="ch11-idx-1000004802-1"></A><ACLASS="indexterm"NAME="ch11-idx-1000004802-2"></A></A></H3><PCLASS="para">For each key in a hash, only one scalar value is allowed, but you'd like to use one key to store and retrieve multiple values. That is, you'd like the value to be a list.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-338">Solution</A></H3><PCLASS="para">Use references to arrays as the hash values. Use <CODECLASS="literal">push</CODE> to append:</P><PRECLASS="programlisting">push(@{ $hash{"KEYNAME"} }, "new value");</PRE><PCLASS="para">Then, dereference the value as an array reference when printing out the hash:</P><PRECLASS="programlisting">foreach $string (keys %hash) { print "$string: @{$hash{$string}}\n"; } </PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-354">Discussion</A></H3><PCLASS="para">You can only store scalar values in a hash. References, however, are scalars. This solves the problem of storing multiple values for one key by making <CODECLASS="literal">$hash{$key}</CODE> a reference to an array containing the values for <CODECLASS="literal">$key</CODE>. The normal hash operations (insertion, deletion, iteration, and testing for existence) can now be written in terms of array operations like <CODECLASS="literal">push</CODE>, <CODECLASS="literal">splice</CODE>, and <CODECLASS="literal">foreach</CODE>.</P><PCLASS="para">Here's how to give a key many values:</P><PRECLASS="programlisting">$hash{"a key"} = [ 3, 4, 5 ]; # anonymous array</PRE><PCLASS="para">Once you have a key with many values, here's how to use them:</P><PRECLASS="programlisting">@values = @{ $hash{"a key"} };</PRE><PCLASS="para">To append a new value to the array of values associated with a particular key, use <CODECLASS="literal">push</CODE><ACLASS="indexterm"NAME="ch11-idx-1000004808-0"></A>:</P><PRECLASS="programlisting">push @{ $hash{"a key"} }, $value;</PRE><PCLASS="para">The classic application of these data structures is inverting a hash that has many keys with the same associated value. When inverted, you end up with a hash that has many values for the same key. This is addressed in <ACLASS="xref"HREF="ch05_09.htm"TITLE="Inverting a Hash">Recipe 5.8</A>.</P><PCLASS="para">Be warned that this:</P><PRECLASS="programlisting">@residents = @{ $phone2name{$number} };</PRE><PCLASS="para">causes a runtime exception under <CODECLASS="literal">use</CODE> <CODECLASS="literal">strict</CODE> because you're dereferencing an undefined reference where autovivification won't occur. You must do this instead:</P><PRECLASS="programlisting">@residents = exists( $phone2name{$number} ) ? @{ $phone2name{$number} } : ();</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch11-pgfId-390">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch04_07.htm#PERL2-CH-4-SECT-7.2">"Hashs of Arrays"</A> in <ACLASS="olink"HREF="../prog/ch04_01.htm">Chapter 4</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perldsc </I>(1); <ACLASS="xref"HREF="ch05_09.htm"TITLE="Inverting a Hash">Recipe 5.8</A>; the example "Tie Example: Make a Hash That Always Appends" in <ACLASS="xref"HREF="ch13_16.htm"TITLE="Creating Magic Variables with tie">Recipe 13.15</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="ch11_02.htm"TITLE="11.1. Taking References to Arrays"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 11.1. Taking References to Arrays"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="ch11_04.htm"TITLE="11.3. Taking References to Hashes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 11.3. Taking References to Hashes"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">11.1. Taking References to Arrays</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">11.3. Taking References to Hashes</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 + -