ch09_02.htm
来自「编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,」· HTM 代码 · 共 221 行
HTM
221 行
<html><head><title>Hashes of Arrays (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Hashes of Arrays"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch09_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch09_01.htm">Chapter 9: Data Structures</a></td><td align="right" valign="top" width="172"><a href="ch09_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">9.2. Hashes of Arrays</h2><p><a name="INDEX-2113"></a><a name="INDEX-2114"></a><a name="INDEX-2115"></a><a name="INDEX-2116"></a>Use a hash of arrays when you want to look up each array by aparticular string rather than merely by an index number. In ourexample of television characters, instead of looking up the list ofnames by the zeroth show, the first show, and so on, we'll set it upso we can look up the cast list given the name of the show.</p><p>Because our outer data structure is a hash, we can't order thecontents, but we can use the <tt class="literal">sort</tt> function to specify a particularoutput order.</p><h3 class="sect2">9.2.1. Composition of a Hash of Arrays</h3><p><a name="INDEX-2117"></a>You can create a hash of anonymous arrays as follows:<blockquote><pre class="programlisting"># We customarily omit quotes when the keys are identifiers.%HoA = ( flintstones => [ "fred", "barney" ], jetsons => [ "george", "jane", "elroy" ], simpsons => [ "homer", "marge", "bart" ],);</pre></blockquote>To add another array to the hash, you can simply say:<blockquote><pre class="programlisting">$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];</pre></blockquote></p><h3 class="sect2">9.2.2. Generation of a Hash of Arrays</h3><p><a name="INDEX-2118"></a>Here are some techniques for populating a hash of arrays.To read from a file with the following format:<blockquote><pre class="programlisting">flintstones: fred barney wilma dinojetsons: george jane elroysimpsons: homer marge bart</pre></blockquote>you could use either of the following two loops:<blockquote><pre class="programlisting">while ( <> ) { next unless s/^(.*?):\s*//; $HoA{$1} = [ split ];}while ( $line = <> ) { ($who, $rest) = split /:\s*/, $line, 2; @fields = split ' ', $rest; $HoA{$who} = [ @fields ];}</pre></blockquote>If you have a subroutine <tt class="literal">get_family</tt> that returns an array,you can use it to stuff <tt class="literal">%HoA</tt> with either of these two loops:<blockquote><pre class="programlisting">for $group ( "simpsons", "jetsons", "flintstones" ) { $HoA{$group} = [ get_family($group) ];}for $group ( "simpsons", "jetsons", "flintstones" ) { @members = get_family($group); $HoA{$group} = [ @members ];}</pre></blockquote>You can append new members to an existing array like so:<blockquote><pre class="programlisting">push @{ $HoA{flintstones} }, "wilma", "pebbles";</pre></blockquote></p><h3 class="sect2">9.2.3. Access and Printing of a Hash of Arrays</h3><p><a name="INDEX-2119"></a><a name="INDEX-2120"></a><a name="INDEX-2121"></a>You can set the first element of a particular array as follows:<blockquote><pre class="programlisting">$HoA{flintstones}[0] = "Fred";</pre></blockquote>To capitalize the second Simpson, apply a substitution to theappropriate arrayelement:<blockquote><pre class="programlisting">$HoA{simpsons}[1] =~ s/(\w)/\u$1/;</pre></blockquote>You can print all of the families by looping through the keys of thehash:<blockquote><pre class="programlisting">for $family ( keys %HoA ) { print "$family: @{ $HoA{$family} }\n";}</pre></blockquote><a name="INDEX-2122"></a>With a little extra effort, you can add array indices as well:<blockquote><pre class="programlisting">for $family ( keys %HoA ) { print "$family: "; for $i ( 0 .. $#{ $HoA{$family} } ) { print " $i = $HoA{$family}[$i]"; } print "\n";}</pre></blockquote><a name="INDEX-2123"></a>Or sort the arrays by how many elements they have:<blockquote><pre class="programlisting">for $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) { print "$family: @{ $HoA{$family} }\n"}</pre></blockquote>Or even sort the arrays by the number of elements and then orderthe elements ASCIIbetically (or to be precise, utf8ically):<blockquote><pre class="programlisting"># Print the whole thing sorted by number of members and name.for $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) { print "$family: ", join(", ", sort @{ $HoA{$family} }), "\n";}</pre></blockquote></p><a name="INDEX-2124"></a><a name="INDEX-2125"></a><a name="INDEX-2126"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch09_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch09_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">9.1. Arrays of Arrays</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">9.3. Arrays of Hashes</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?